Preg_drugchat / app.py
leonsimon23's picture
Update app.py
1fed798 verified
import base64
import time
from io import BytesIO
import chainlit as cl
from chainlit.element import ElementBased
from chainlit.input_widget import Select, Slider, Switch
import openai
import os
# 配置 OpenAI
openai.api_base = os.getenv('OPENAI_BASE_URL')
openai.api_key = os.getenv('OPENAI_API_KEY')
@cl.on_chat_start
async def start_chat():
content = "你好,我是泰山AI智能客服,有什么可以帮助您吗?"
msg = cl.Message(content="")
for token in content:
time.sleep(0.2)
await msg.stream_token(token)
await msg.send()
@cl.on_message
async def main(message: cl.Message):
msg = cl.Message(content="", author="tarzan")
await msg.send()
try:
response = await openai.ChatCompletion.acreate(
model="gpt-3.5", # 这里随便写,fastgpt接口最终使用的是你后台配置的模型
messages=cl.chat_context.to_openai(),
stream=True
)
async for chunk in response:
if token := chunk['choices'][0]['delta'].get('content'):
await msg.stream_token(token)
except Exception as e:
await msg.stream_token(f"Error: {str(e)}")
await msg.update()