|
|
from fastapi import FastAPI |
|
|
from pydantic import BaseModel |
|
|
|
|
|
import gradio as gr |
|
|
from groq import Groq |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
class Item(BaseModel): |
|
|
prompt: str |
|
|
system_prompt: str = "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. Your response should be complete, and not end abruptly." |
|
|
|
|
|
@app.post("/chat") |
|
|
def gradio_chat(item: Item): |
|
|
return {"result": chat_with_llama(item.prompt,item.system_prompt)} |
|
|
|
|
|
client = Groq(api_key=os.environ.get("groq_api_key")) |
|
|
|
|
|
def chat_with_llama(prompt,system_prompt): |
|
|
chat_completion = client.chat.completions.create( |
|
|
messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": prompt}], |
|
|
model="llama-3.1-8b-instant", |
|
|
) |
|
|
|
|
|
return chat_completion.choices[0].message.content |
|
|
|
|
|
def clear_fields(): |
|
|
return "", "", "" |
|
|
|
|
|
def reset_button(): |
|
|
return "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. Your response should be complete, and not end abruptly." |
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(theme='freddyaboulton/test-blue') as demo: |
|
|
gr.Markdown("<center><h2>Arjun's Chatbot</h2></center>") |
|
|
gr.Markdown("Hi there! I'm an AI assistant tasked with answering one question at a time. I'm instructed to provide complete and truthful responses, without any harmful or biased content. I have some memory issues which are work in progress, so I cannot remember previous conversations, yet!. Happy Chatting!") |
|
|
prompt = gr.Textbox(label='Question', lines=2, max_lines=5, placeholder = 'Type your question here.') |
|
|
system_prompt = gr.Textbox(label='System Prompt', lines=2, max_lines=10, value="You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. Your response should be complete, and not end abruptly.") |
|
|
|
|
|
with gr.Group(): |
|
|
with gr.Row(): |
|
|
submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm") |
|
|
clear_btn = gr.ClearButton(value="Clear Question and AI Response", elem_id="clear_button", variant="secondary", size="sm") |
|
|
reset_btn = gr.Button(value='Reset System Prompt', elem_id="reset_button", variant='secondary', size="sm") |
|
|
|
|
|
with gr.Row() as output: |
|
|
llm_output = gr.Markdown("<center><h3>AI Response</h3></center>") |
|
|
|
|
|
|
|
|
submit_btn.click(fn=chat_with_llama, inputs = [prompt,system_prompt], outputs=[llm_output]) |
|
|
clear_btn.click(fn=clear_fields,outputs=[prompt,llm_output,system_prompt]) |
|
|
reset_btn.click(fn=reset_button, outputs=[system_prompt]) |
|
|
|
|
|
app = gr.mount_gradio_app(app, demo, path="/") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
import uvicorn |
|
|
uvicorn.run(app, host="0.0.0.0", port=7860) |
|
|
|
|
|
|
|
|
|
|
|
|