It is so easy to create a simple chatbot interface for @ollama using @Gradio
https://x.com/RyanMorrisonJer/status/1849911680504889685
#ollama #Gradio
code:python
import gradio as gr
import ollama
def format_history(query, history, system_prompt):
chat_history = {"role": "system", "content": system_prompt}
for h in history:
chat_history.append({"role": "user", "content": h0})
chat_history.append({"role": "assistant", "content": h1})
chat_history.append({"role": "user", "content": query})
return chat_history
def generate_response(query, history):
messages = format_history(query, history, "You are a helpful assistant")
response = ollama.chat(
model='llama3.2:3b',
messages=messages,
stream=True
)
message = ""
for chunk in response:
message += chunk'message''content'
yield message
iface = gr.ChatInterface(
fn=generate_response,
title="Ollama Chat",
examples="Tell me about yourself", "What can you do?",
)
iface.launch()