chainlit_test / app.py
Bhavesh7895's picture
Update app.py
bf63868 verified
raw
history blame contribute delete
905 Bytes
import asyncio
import chainlit as cl
from together import Together
TOGETHER_API_KEY = "4e381cebe7224f3da54cae6e54d3fdd3ee00b6ac160fc29cea66fbe48a0be3d1"
@cl.on_message
async def main(message: str):
if not TOGETHER_API_KEY:
await cl.Message(content="Error: No Together API key provided.").send()
return
client = Together(api_key=TOGETHER_API_KEY)
messages = [{"role": "user", "content": message}]
try:
# Run the synchronous API call in a thread
completion = await asyncio.to_thread(
client.chat.completions.create,
model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
messages=messages,
max_tokens=500
)
response = completion.choices[0].message.content
await cl.Message(content=response).send()
except Exception as e:
await cl.Message(content=f"Error: {str(e)}").send()