Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
|
|
1 year ago | |
|---|---|---|
| .. | ||
| .gitignore | 1 year ago | |
| README.md | 1 year ago | |
| app.py | 1 year ago | |
| model_config_template.yaml | 1 year ago | |
This sample demonstrates how to build a streaming chat API with multi-turn conversation history using autogen-core and FastAPI.
StreamingResponse, autogen-core's asynchronous features, and a global queue created with asyncio.Queue() to manage the data stream, thereby providing faster user-perceived response times.MyAgent) can receive and process chat history records (ChatHistory) containing multiple turns of interaction, enabling context-aware continuous conversations.app.py: FastAPI application code, including API endpoints, Agent definitions, runtime settings, and streaming logic.README.md: (This document) Project introduction and usage instructions.First, make sure you have Python installed (recommended 3.8 or higher). Then, in your project directory, install the necessary libraries via pip:
pip install "fastapi" "uvicorn[standard]" "autogen-core" "autogen-ext[openai]"
Create a new file named model_config.yaml in the same directory as this README file to configure your model settings.
See model_config_template.yaml for an example.
Note: Hardcoding API keys directly in the code is only suitable for local testing. For production environments, it is strongly recommended to use environment variables or other secure methods to manage keys.
In the directory containing app.py, run the following command to start the FastAPI application:
uvicorn app:app --host 0.0.0.0 --port 8501 --reload
After the service starts, the API endpoint will be available at http://<your-server-ip>:8501/chat/completions.
You can interact with the Agent by sending a POST request to the /chat/completions endpoint. The request body must be in JSON format and contain a messages field, the value of which is a list, where each element represents a turn of conversation.
Request Body Format:
{
"messages": [
{"source": "user", "content": "Hello!"},
{"source": "assistant", "content": "Hello! How can I help you?"},
{"source": "user", "content": "Introduce yourself."}
]
}
Example (using curl):
curl -N -X POST http://localhost:8501/chat/completions \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"source": "user", "content": "Hello, I'\''m Tory."},
{"source": "assistant", "content": "Hello Tory, nice to meet you!"},
{"source": "user", "content": "Say hello by my name and introduce yourself."}
]
}'
Example (using Python requests):
import requests
import json
url = "http://localhost:8501/chat/completions"
data = {
'stream': True,
'messages': [
{'source': 'user', 'content': "Hello,I'm tory."},
{'source': 'assistant', 'content':"hello Tory, nice to meet you!"},
{'source': 'user', 'content': "Say hello by my name and introduce yourself."}
]
}
headers = {'Content-Type': 'application/json'}
try:
response = requests.post(url, json=data, headers=headers, stream=True)
response.raise_for_status()
for chunk in response.iter_content(chunk_size=None):
if chunk:
print(json.loads(chunk)["content"], end='', flush=True)
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
except json.JSONDecodeError as e:
print(f"JSON Decode Error: {e}")
This is a mirror of AutoGen from GitHub. AutoGen is a framework that enables the development of LLM applications using multiple agents that can converse with each other to solve tasks.
Python SVG Jupyter Notebook C# TSX other