-
Notifications
You must be signed in to change notification settings - Fork 5k
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
OpenAI Assistants Agent #4131
OpenAI Assistants Agent #4131
Conversation
@ekzhu @jackgerrits , this is a very early draft, I have some questions before proceeding further.
|
Thanks. I think we can follow the design in the Core cookbook for open ai assistant agent: https://microsoft.github.io/autogen/dev/user-guide/core-user-guide/cookbook/openai-assistant-agent.html. The API should be simple without introducing additional abstractions on our side.
We don't need to introduce additional abstractions because OpenAI Assistant is specific to OpenAI and Azure OpenAI services -- we should stick with the official clients they provide. Furthermore, we shouldn't expect the agent to be the only interface to the assistant features such as file search, and as the application may also perform other functions such as file upload and thread management.
Use the official
This should be mostly done using the official
We should make sure we can use our Overall, the goal is to bring OpenAI assistant agents into our ecosystem, not to build a new wrapper around assistant API. |
@ekzhu , this is ready for review. I tested it with the script below. I haven't added unit tests as mocking the assistants api will take a lot of effort and we dont have other use cases. import asyncio
from enum import Enum
from typing import List, Optional
from autogen_agentchat.agents import OpenAIAssistantAgent
from autogen_core.components.tools._base import BaseTool
from openai import AsyncAzureOpenAI
from autogen_agentchat.messages import TextMessage
from autogen_core.base import CancellationToken
from pydantic import BaseModel
class QuestionType(str, Enum):
MULTIPLE_CHOICE = "MULTIPLE_CHOICE"
FREE_RESPONSE = "FREE_RESPONSE"
class Question(BaseModel):
question_text: str
question_type: QuestionType
choices: Optional[List[str]] = None
class DisplayQuizArgs(BaseModel):
title: str
questions: List[Question]
# Step 2: Create the Tool class by subclassing BaseTool
class DisplayQuizTool(BaseTool[DisplayQuizArgs, List[str]]):
def __init__(self):
super().__init__(
args_type=DisplayQuizArgs,
return_type=List[str],
name="display_quiz",
description=(
"Displays a quiz to the student and returns the student's responses. "
"A single quiz can have multiple questions."
),
)
# Step 3: Implement the run method
async def run(self, args: DisplayQuizArgs, cancellation_token: CancellationToken) -> List[str]:
# Simulate displaying the quiz and collecting responses
responses = []
for q in args.questions:
if q.question_type == QuestionType.MULTIPLE_CHOICE:
# Simulate a response for multiple-choice questions
response = q.choices[0] if q.choices else ""
elif q.question_type == QuestionType.FREE_RESPONSE:
# Simulate a response for free-response questions
response = "Sample free response"
else:
response = ""
responses.append(response)
return responses
def create_agent(client: AsyncAzureOpenAI) -> OpenAIAssistantAgent:
tools = [
{"type": "code_interpreter"},
{"type": "file_search"},
{"type": "tool", "tool": DisplayQuizTool()},
]
return OpenAIAssistantAgent(
name="assistant",
instructions="Help the user with their task.",
model="gpt-4o-mini",
description="OpenAI Assistant Agent",
client=client,
tools=tools,
)
async def test_file_retrieval(agent: OpenAIAssistantAgent, cancellation_token: CancellationToken):
file_path = r".\data\SampleBooks\jungle_book.txt"
await agent.on_upload_for_file_search(file_path, cancellation_token)
message = TextMessage(source="user", content="What is the first sentence of the jungle scout book?")
response = await agent.on_messages([message], cancellation_token)
print("File Retrieval Test Response:", response.chat_message.content)
await agent.delete_uploaded_files(cancellation_token)
await agent.delete_vector_store(cancellation_token)
async def test_code_interpreter(agent: OpenAIAssistantAgent, cancellation_token: CancellationToken):
message = TextMessage(source="user", content="I need to solve the equation `3x + 11 = 14`. Can you help me?")
response = await agent.on_messages([message], cancellation_token)
print("Code Interpreter Test Response:", response.chat_message.content)
async def test_quiz_creation(agent: OpenAIAssistantAgent, cancellation_token: CancellationToken):
message = TextMessage(source="user", content="Create a short quiz about basic math with one multiple choice question and one free response question.")
response = await agent.on_messages([message], cancellation_token)
print("Quiz Creation Test Response:", response.chat_message.content)
async def main():
client = AsyncAzureOpenAI(
azure_endpoint="https://{your-api-endpoint}.openai.azure.com",
api_version="2024-08-01-preview",
api_key="your-api-key"
)
cancellation_token = CancellationToken()
# Test file retrieval
agent = create_agent(client)
await test_file_retrieval(agent, cancellation_token)
await agent.delete_assistant(cancellation_token)
# Wait 1 minute before next test
await asyncio.sleep(60)
# Test code interpreter
agent = create_agent(client)
await test_code_interpreter(agent, cancellation_token)
await agent.delete_assistant(cancellation_token)
# Wait 1 minute before next test
await asyncio.sleep(60)
# Test quiz creation
agent = create_agent(client)
await test_quiz_creation(agent, cancellation_token)
await agent.delete_assistant(cancellation_token)
if __name__ == "__main__":
asyncio.run(main()) Here is the output of the tool call inspection -> run = await cancellation_token.link_future(
(Pdb) tool_outputs
[FunctionExecutionResult(content="['4', 'Sample free response']", call_id='call_mx2T4F0niQvJ0FiGBqsMGZXl'), FunctionExecutionResult(content="['4', 'Sample free response']", call_id='call_WW4D3OtQteig5BwH5FOZhdgL')]
(Pdb) c
Quiz Creation Test Response: I have created a short quiz about basic math. Here it is:
### Basic Math Quiz
1. **What is 15 divided by 3?**
- A) 4
- B) 5
- C) 6
- D) 7
2. **What is the square root of 64?**
- (Free Response) |
Why are these changes needed?
Related issue number
Checks