[Python] Passing request context from FastAPI to Microsoft Semantic Kernel Plugin for OpenAI...

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I am integrating Microsoft Semantic Kernel with OpenAI in my FastAPI application. I have a chat/ endpoint where I receive a session_id from the request, and I need to pass this session_id to a plugin along with the openai_client. However, I'm unsure how to properly pass the session_id from the FastAPI request to the plugin inside the kernel's execution process.

    Here is the relevant code for setting up the kernel and plugin:

    # Kernel and service setup
    kernel = Kernel()

    execution_settings = AzureChatPromptExecutionSettings(tool_choice="auto")
    execution_settings.function_choice_behavior = FunctionChoiceBehavior.Auto(filters={})

    openai_client = OpenAI(api_key=api_key)
    chat_completion_service = OpenAIChatCompletion(
    ai_model_id=model_id,
    api_key=api_key,
    service_id=service_id
    )

    # Add services and plugins
    kernel.add_service(chat_completion_service)
    kernel.add_plugin(MovesPlugin(openai_client), plugin_name='MovesPlugin')


    Inside my FastAPI endpoint, I want to pass the session_id to the plugin when calling the kernel for a chat response:

    # Inside FastAPI endpoint
    @app.post("/chat/")
    async def chat(request: Request):
    session_id = await request.json().get('session_id')

    # Get chat completion service
    _chat_completion_service = kernel.get_service(type=ChatCompletionClientBase)

    # Get chat completion response
    response = await _chat_completion_service.get_chat_message_content(
    chat_history=chat_history,
    kernel=kernel,
    settings=execution_settings
    )

    return response


    How can I pass the request context (session_id) from the FastAPI request to the MovesPlugin and make sure it is used correctly along with the openai_client inside the Semantic Kernel execution?

    Any guidance or suggestions would be greatly appreciated!

    Continue reading...

Compartilhe esta Página