File size: 905 Bytes
bf63868
957fe87
89e67c5
957fe87
414cee7
 
957fe87
 
bf63868
 
957fe87
 
bf63868
414cee7
957fe87
 
bf63868
 
 
89e67c5
957fe87
 
 
 
 
 
89e67c5
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
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()