| |
| """app.ipynb |
| |
| Automatically generated by Colab. |
| |
| Original file is located at |
| https://colab.research.google.com/drive/1AjpNoqD5qJvJINeELd-yNZuVRP88Zt3G |
| """ |
|
|
| |
|
|
| import os |
| from groq import Groq |
| import gradio as gr |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| client = Groq() |
|
|
| |
| MODEL_NAME = "llama-3.1-8b-instant" |
|
|
|
|
| def chat_with_groq(message, history): |
| """ |
| message: latest user input (string) |
| history: list of [user, assistant] pairs from Gradio |
| returns: assistant reply (string) |
| """ |
|
|
| |
| messages = [] |
| for user_msg, bot_msg in history: |
| messages.append({"role": "user", "content": user_msg}) |
| if bot_msg is not None: |
| messages.append({"role": "assistant", "content": bot_msg}) |
|
|
| |
| messages.append({"role": "user", "content": message}) |
|
|
| |
| response = client.chat.completions.create( |
| model=MODEL_NAME, |
| messages=messages, |
| temperature=0.7, |
| max_tokens=512, |
| ) |
|
|
| reply = response.choices[0].message.content |
| return reply |
|
|
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# 💬 Groq Llama Chatbot") |
| gr.Markdown( |
| "Chat with a Llama model served through the Groq API. " |
| "Make sure you set your `GROQ_API_KEY` before running." |
| ) |
|
|
| chatbot = gr.Chatbot(height=400) |
| msg = gr.Textbox(label="Type your message here") |
| clear = gr.Button("Clear") |
|
|
| def user_send(user_message, chat_history): |
| |
| chat_history = chat_history + [[user_message, None]] |
| return "", chat_history |
|
|
| def bot_reply(chat_history): |
| user_message = chat_history[-1][0] |
| bot_answer = chat_with_groq(user_message, chat_history[:-1]) |
| chat_history[-1][1] = bot_answer |
| return chat_history |
|
|
| msg.submit(user_send, [msg, chatbot], [msg, chatbot]).then( |
| bot_reply, [chatbot], [chatbot] |
| ) |
| clear.click(lambda: None, None, chatbot, queue=False) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |
|
|
| import os |
| from groq import Groq |
| import gradio as gr |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| client = Groq(api_key=os.environ.get("GROQ_API_KEY")) |
|
|
| |
| MODEL_NAME = "llama-3.3-70b-versatile" |
|
|
|
|
| def chat_with_groq(message, history): |
| """ |
| message: latest user input (string) |
| history: list of [user, assistant] pairs from Gradio |
| returns: assistant reply (string) |
| """ |
|
|
| |
| messages = [] |
| for user_msg, bot_msg in history: |
| messages.append({"role": "user", "content": user_msg}) |
| if bot_msg is not None: |
| messages.append({"role": "assistant", "content": bot_msg}) |
|
|
| |
| messages.append({"role": "user", "content": message}) |
|
|
| |
| response = client.chat.completions.create( |
| model=MODEL_NAME, |
| messages=messages, |
| temperature=0.7, |
| max_tokens=512, |
| ) |
|
|
| reply = response.choices[0].message.content |
| return reply |
|
|
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# 💬 Groq Llama Chatbot") |
| gr.Markdown( |
| "Chat with a Llama model served through the Groq API. " |
| "Make sure you set your `GROQ_API_KEY` before running." |
| ) |
|
|
| chatbot = gr.Chatbot(height=400) |
| msg = gr.Textbox(label="Type your message here") |
| clear = gr.Button("Clear") |
|
|
| def user_send(user_message, chat_history): |
| |
| chat_history = chat_history + [[user_message, None]] |
| return "", chat_history |
|
|
| def bot_reply(chat_history): |
| user_message = chat_history[-1][0] |
| bot_answer = chat_with_groq(user_message, chat_history[:-1]) |
| chat_history[-1][1] = bot_answer |
| return chat_history |
|
|
| msg.submit(user_send, [msg, chatbot], [msg, chatbot]).then( |
| bot_reply, [chatbot], [chatbot] |
| ) |
| clear.click(lambda: None, None, chatbot, queue=False) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |