test_app_01.py 872 字节
Newer Older
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
import openai
import chainlit as cl

openai.api_key = "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"
model_name = "text-davinci-003"
settings = {
    "temperature": 0.7,
    "max_tokens": 500,
    "top_p": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0,
    "stop": ["```"],
}
prompt = """Answer the following question:
{question}
"""


@cl.on_message
async def main(message: str):
    fromatted_prompt = prompt.format(question=message)
    msg = cl.Message(
        content="",
        prompt=fromatted_prompt,
        llm_settings=cl.LLMSettings(model_name=model_name, **settings),
    )
    async for stream_resp in await openai.Completion.acreate(
            model=model_name, prompt=fromatted_prompt, stream=True, **settings
    ):
        token = stream_resp.get("choices")[0].get("text")
        await msg.stream_token(token)
    await msg.send()