-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
58 lines (45 loc) · 1.51 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from typing import List
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from openai import AzureOpenAI
import config
app = FastAPI()
# Initialize AzureOpenAI client
client = AzureOpenAI(
azure_endpoint=config.azure_openai_endpoint,
api_key=config.azure_openai_key_key,
api_version=config.azure_api_version,
)
class Message(BaseModel):
role: str
content: str
class ChatRequest(BaseModel):
messages: List[Message]
def stream_response(messages: List[Message]):
response = client.chat.completions.create(
model=config.azure_openai_deployment_name,
messages=[message.model_dump() for message in messages],
temperature=0,
stream=True
)
for chunk in response:
if chunk.choices:
delta = chunk.choices[0].delta
if hasattr(delta, 'content') and delta.content:
yield delta.content.encode('utf-8')
@app.post("/stream-chat/")
async def stream_chat(chat_request: ChatRequest):
return StreamingResponse(stream_response(chat_request.messages))
# Run the app with: uvicorn this_file_name:app --reload
# Test the app with:
# ❯ curl -X 'POST' \
# 'http://127.0.0.1:8000/stream-chat/' \
# -H 'accept: application/json' \
# -H 'Content-Type: application/json' \
# -d '{
# "messages": [
# {"role": "system", "content": "You are a helpful assistant."},
# {"role": "user", "content": "write 20 sentence about cow"}
# ]
# }'