ollama==0.4.7 gradio==5.14.0
import gradio as gr from ollama import Client client = Client(host="http://192.168.11.18:11434") system_prompt_text = "You are an excellent assistant to the programmer." init = { "role": "system", "content": system_prompt_text, } def user( message: str, history: list[dict] ): if len(history)==0: history.insert(0, init) history.append( { "role": "user", "content": message } ) return "", history def bot( history: list[dict] ): stream = client.chat( model="falcon3", messages=history, stream=True ) history.append({"role": "assistant", "content": ""}) for new_text in stream: history[-1]["content"] += new_text.message.content yield history with gr.Blocks() as demo: chatbot = gr.Chatbot(type="messages") msg = gr.Textbox() clear = gr.ClearButton([msg, chatbot], value="新しいチャットを開始") msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then( bot, chatbot, chatbot ) demo.launch()
関連記事
こちらはTransformersとGradioを使った場合です。touch-sp.hatenablog.com