Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,42 +4,71 @@
|
|
| 4 |
|
| 5 |
import openai #importing openai for API usage
|
| 6 |
import chainlit as cl #importing chainlit for our app
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# You only need the api key inserted here if it's not in your .env file
|
| 9 |
#openai.api_key = "YOUR_API_KEY"
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"temperature": 0.7, # higher value increases output diveresity/randomness
|
| 17 |
-
"max_tokens": 500, # maximum length of output response
|
| 18 |
-
"top_p": 1, # choose only the top x% of possible words to return
|
| 19 |
-
"frequency_penalty": 0, # higher value will result in the model being more conservative in its use of repeated tokens.
|
| 20 |
-
"presence_penalty": 0, # higher value will result in the model being more likely to generate tokens that have not yet been included in the generated text
|
| 21 |
-
}
|
| 22 |
|
| 23 |
@cl.on_chat_start # marks a function that will be executed at the start of a user session
|
| 24 |
-
def start_chat():
|
| 25 |
-
|
| 26 |
-
"
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
| 30 |
|
| 31 |
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
| 32 |
async def main(message: str):
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
msg = cl.Message(content="")
|
| 37 |
|
|
|
|
| 38 |
async for stream_resp in await openai.ChatCompletion.acreate(
|
| 39 |
-
|
| 40 |
):
|
| 41 |
token = stream_resp.choices[0]["delta"].get("content", "")
|
| 42 |
await msg.stream_token(token)
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
await msg.send()
|
|
|
|
| 4 |
|
| 5 |
import openai #importing openai for API usage
|
| 6 |
import chainlit as cl #importing chainlit for our app
|
| 7 |
+
from chainlit.input_widget import Select, Switch, Slider #importing chainlit settings selection tools
|
| 8 |
+
from chainlit.prompt import Prompt, PromptMessage #importing prompt tools
|
| 9 |
+
from chainlit.playground.providers import ChatOpenAI #importing ChatOpenAI tools
|
| 10 |
|
| 11 |
# You only need the api key inserted here if it's not in your .env file
|
| 12 |
#openai.api_key = "YOUR_API_KEY"
|
| 13 |
|
| 14 |
+
# ChatOpenAI Templates
|
| 15 |
+
system_template = """You are a helpful assistant who always speaks in a pleasant tone!
|
| 16 |
+
"""
|
| 17 |
|
| 18 |
+
user_template = """{input}
|
| 19 |
+
Think through your response step by step.
|
| 20 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@cl.on_chat_start # marks a function that will be executed at the start of a user session
|
| 23 |
+
async def start_chat():
|
| 24 |
+
settings = {
|
| 25 |
+
"model": "gpt-3.5-turbo",
|
| 26 |
+
"temperature": 0,
|
| 27 |
+
"max_tokens": 500,
|
| 28 |
+
"top_p": 1,
|
| 29 |
+
"frequency_penalty": 0,
|
| 30 |
+
"presence_penalty": 0,
|
| 31 |
+
}
|
| 32 |
|
| 33 |
+
cl.user_session.set("settings", settings)
|
| 34 |
|
| 35 |
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
| 36 |
async def main(message: str):
|
| 37 |
+
|
| 38 |
+
settings = cl.user_session.get("settings")
|
| 39 |
+
|
| 40 |
+
prompt = Prompt(
|
| 41 |
+
provider=ChatOpenAI.id,
|
| 42 |
+
messages=[
|
| 43 |
+
PromptMessage(
|
| 44 |
+
role="system",
|
| 45 |
+
template=system_template,
|
| 46 |
+
formatted=system_template,
|
| 47 |
+
),
|
| 48 |
+
PromptMessage(
|
| 49 |
+
role="user",
|
| 50 |
+
template=user_template,
|
| 51 |
+
formatted=user_template.format(input=message),
|
| 52 |
+
)
|
| 53 |
+
],
|
| 54 |
+
inputs = {"input" : message},
|
| 55 |
+
settings=settings
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
print([m.to_openai() for m in prompt.messages])
|
| 59 |
|
| 60 |
msg = cl.Message(content="")
|
| 61 |
|
| 62 |
+
# Call OpenAI
|
| 63 |
async for stream_resp in await openai.ChatCompletion.acreate(
|
| 64 |
+
messages=[m.to_openai() for m in prompt.messages], stream=True, **settings
|
| 65 |
):
|
| 66 |
token = stream_resp.choices[0]["delta"].get("content", "")
|
| 67 |
await msg.stream_token(token)
|
| 68 |
|
| 69 |
+
# Update the prompt object with the completion
|
| 70 |
+
prompt.completion = msg.content
|
| 71 |
+
msg.prompt = prompt
|
| 72 |
+
|
| 73 |
+
# Send and close the message stream
|
| 74 |
await msg.send()
|