feat: body param added

This commit is contained in:
Timothy J. Baek 2024-05-21 19:20:38 -07:00
parent 1fa58ecc1c
commit ee4544d4f9
8 changed files with 66 additions and 8 deletions

12
main.py
View File

@ -129,7 +129,11 @@ async def generate_openai_chat_completion(form_data: OpenAIChatCompletionForm):
if form_data.stream:
def stream_content():
res = get_response(user_message, messages=form_data.messages)
res = get_response(
user_message,
messages=form_data.messages,
body=form_data.model_dump_json(),
)
print(f"stream:true:{res}")
@ -164,7 +168,11 @@ async def generate_openai_chat_completion(form_data: OpenAIChatCompletionForm):
return StreamingResponse(stream_content(), media_type="text/event-stream")
else:
res = get_response(user_message, messages=form_data.messages)
res = get_response(
user_message,
messages=form_data.messages,
body=form_data.model_dump_json(),
)
print(f"stream:false:{res}")
message = ""

View File

@ -79,7 +79,7 @@ class Pipeline:
pass
def get_response(
self, user_message: str, messages: List[OpenAIChatMessage]
self, user_message: str, messages: List[OpenAIChatMessage], body: dict
) -> Union[str, Generator]:
# This is where you can add your custom RAG pipeline.
# Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response.

View File

@ -70,7 +70,7 @@ class Pipeline:
pass
def get_response(
self, user_message: str, messages: List[OpenAIChatMessage]
self, user_message: str, messages: List[OpenAIChatMessage], body: dict
) -> Union[str, Generator]:
# This is where you can add your custom RAG pipeline.
# Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response.

View File

@ -30,7 +30,7 @@ class Pipeline:
pass
def get_response(
self, user_message: str, messages: List[OpenAIChatMessage]
self, user_message: str, messages: List[OpenAIChatMessage], body: dict
) -> Union[str, Generator]:
# This is where you can add your custom RAG pipeline.
# Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response.

View File

@ -25,7 +25,7 @@ class Pipeline:
pass
def get_response(
self, user_message: str, messages: List[OpenAIChatMessage]
self, user_message: str, messages: List[OpenAIChatMessage], body: dict
) -> Union[str, Generator]:
# This is where you can add your custom RAG pipeline.
# Typically, you would retrieve relevant information from your knowledge base and synthesize it to generate a response.

View File

@ -0,0 +1,49 @@
from typing import List, Union, Generator
from schemas import OpenAIChatMessage
import requests
class Pipeline:
def __init__(self):
pass
async def on_startup(self):
# This function is called when the server is started.
print(f"on_startup:{__name__}")
pass
async def on_shutdown(self):
# This function is called when the server is stopped.
print(f"on_shutdown:{__name__}")
pass
def get_response(
self, user_message: str, messages: List[OpenAIChatMessage], body: dict
) -> Union[str, Generator]:
# This is where you can add your custom pipelines like RAG.'
print(f"get_response:{__name__}")
print(messages)
print(user_message)
OPENAI_API_KEY = "your-api-key-here"
headers = {}
headers["Authorization"] = f"Bearer {OPENAI_API_KEY}"
headers["Content-Type"] = "application/json"
r = requests.request(
method="POST",
url="https://api.openai.com/v1",
data=body,
headers=headers,
stream=True,
)
r.raise_for_status()
# Check if response is SSE
if "text/event-stream" in r.headers.get("Content-Type", ""):
return r.iter_content(chunk_size=8192)
else:
response_data = r.json()
return f"{response_data['choices'][0]['text']}"

View File

@ -17,12 +17,13 @@ class Pipeline:
pass
def get_response(
self, user_message: str, messages: List[OpenAIChatMessage]
self, user_message: str, messages: List[OpenAIChatMessage], body: dict
) -> Union[str, Generator]:
# This is where you can add your custom pipelines like RAG.'
print(f"get_response:{__name__}")
print(messages)
print(user_message)
print(body)
return f"{__name__} response to: {user_message}"

View File

@ -17,7 +17,7 @@ class Pipeline:
pass
def get_response(
self, user_message: str, messages: List[OpenAIChatMessage]
self, user_message: str, messages: List[OpenAIChatMessage], body: dict
) -> Union[str, Generator]:
# This is where you can add your custom pipelines like RAG.'
print(f"get_response:{__name__}")