-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add demoui for openai api (#777)
add chainlit frontend for openai api
- Loading branch information
Showing
6 changed files
with
83 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import os | ||
from urllib.parse import urljoin | ||
|
||
from openai import AsyncOpenAI | ||
import chainlit as cl | ||
|
||
URL = os.environ.get('WORKSPACE_SERVICE_URL') | ||
|
||
client = AsyncOpenAI(base_url=urljoin(URL, "v1"), api_key="YOUR_OPENAI_API_KEY") | ||
cl.instrument_openai() | ||
|
||
settings = { | ||
"temperature": 0.7, | ||
"max_tokens": 500, | ||
"top_p": 1, | ||
"frequency_penalty": 0, | ||
"presence_penalty": 0, | ||
} | ||
|
||
@cl.on_chat_start | ||
async def start_chat(): | ||
models = await client.models.list() | ||
print(f"Using model: {models}") | ||
if len(models.data) == 0: | ||
raise ValueError("No models found") | ||
|
||
global model | ||
model = models.data[0].id | ||
print(f"Using model: {model}") | ||
|
||
@cl.on_message | ||
async def main(message: cl.Message): | ||
messages=[ | ||
{ | ||
"content": "You are a helpful assistant.", | ||
"role": "system" | ||
}, | ||
{ | ||
"content": message.content, | ||
"role": "user" | ||
} | ||
] | ||
msg = cl.Message(content="") | ||
|
||
stream = await client.chat.completions.create( | ||
messages=messages, model=model, | ||
stream=True, | ||
**settings | ||
) | ||
|
||
async for part in stream: | ||
if token := part.choices[0].delta.content or "": | ||
await msg.stream_token(token) | ||
await msg.update() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters