ai-maker-space/GenZAI
3
1# You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)2 3# OpenAI Chat completion4import os5from openai import AsyncOpenAI # importing openai for API usage6import chainlit as cl # importing chainlit for our app7from chainlit.prompt import Prompt, PromptMessage # importing prompt tools8from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools9from dotenv import load_dotenv10import requests11 12load_dotenv()13 14prompt_template = """\15<|begin_of_text|><|start_header_id|>system<|end_header_id|>16 17Gen-Z-ify<|eot_id|><|start_header_id|>user<|end_header_id|>18 19You must respond using Gen-Z Language: {english}<|eot_id|><|start_header_id|>assistant<|end_header_id|>20"""21 22API_URL = "https://nc7q281oard1b1ar.us-east-1.aws.endpoints.huggingface.cloud"23 24@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user25async def main(message: cl.Message):26 headers = {27 "Accept" : "application/json",28 "Authorization": f"Bearer {os.environ['HF_TOKEN']}",29 "Content-Type": "application/json" 30 }31 32 def query(payload):33 response = requests.post(API_URL, headers=headers, json=payload)34 return response.json()35 36 formatted_prompt = prompt_template.format(english=message.content)37 38 print(formatted_prompt)39 40 output = query({41 "inputs": formatted_prompt,42 "parameters": {43 "return_full_text": False,44 "clean_up_tokenization_spaces": False45 }46 })47 48 msg = cl.Message(content=output[0]["generated_text"])49 50 # Send and close the message stream51 await msg.send()52 