| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
This sample demonstrates how to build a streaming chat API with multi-turn conversation history using autogen-core and FastAPI.
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 --reloadAfter 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}")| Back | FazBrowse Home | New Git URL |