Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support FastAPI as UI using NATS #228

Closed
davorrunje opened this issue Sep 16, 2024 · 2 comments
Closed

Support FastAPI as UI using NATS #228

davorrunje opened this issue Sep 16, 2024 · 2 comments
Assignees

Comments

@davorrunje
Copy link
Contributor

davorrunje commented Sep 16, 2024

From Reddit:

  • https://www.reddit.com/r/AutoGenAI/comments/1fj1yq9/handling_multiple_users_at_the_same_time/

    I have a 3-agent system written in AutoGen. I want to wrap that around in an API and expose that to an existing web app. This is not a chat application. It's an agent that takes in a request and processes it using various specialized agents. The end result is a json. I want this agentic system to be used by 100s of users at the same time. How do I make sure that the agent system i wrote can scale up and can maintain the states of each user connecting to it?

@kumaranvpl
Copy link
Collaborator

import os

from autogen.agentchat import ConversableAgent

from fastagency import UI
from fastagency.ui.console import ConsoleUI, FastAPIUI
from fastagency.runtime.autogen.base import AutoGenWorkflows

from fastagency import FastAgency

llm_config = {
    "config_list": [
        {
            "model": "gpt-4o",
            "api_key": os.getenv("OPENAI_API_KEY"),
        }
    ],
    "temperature": 0.0,
}


wf = AutoGenWorkflows()

@wf.register(name="simple_learning", description="Student and teacher learning chat")
def simple_workflow(wf: AutoGenWorkflows, ui: UI, initial_message: str, session_id: str) -> str:
    student_agent = ConversableAgent(
        name="Student_Agent",
        system_message="You are a student willing to learn.",
        llm_config=llm_config,
    )
    teacher_agent = ConversableAgent(
        name="Teacher_Agent",
        system_message="You are a math teacher.",
        llm_config=llm_config,
    )

    chat_result = student_agent.initiate_chat(
        teacher_agent,
        message=initial_message,
        summary_method="reflection_with_llm",
        max_turns=5,
    )

    return chat_result.summary

ui=FastAPIUI(nats_params=..., no_workers=10)
fa_app = FastAgency(wf=wf, ui=ui)

app = FastAPI(lifespan=fa_app.lifespan(fa_app))

# do whatever you want with the app
...
def some_route():
    web_socket_info = fa_app.run_workflow("simple_learning", "Hello, teacher!", "session_id")

@kumaranvpl
Copy link
Collaborator

  • Gen JWT with FastAPI and use it to authenticate nats
  • initiate_chat should have user_id in its subject name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants