我正在使用 Ollama 通过 Ollama Python API 从大型语言模型 (LLM) 生成答案。我想通过单击停止按钮取消响应生成。问题在于,只有当响应生成已经开始打印时,任务取消才会起作用。如果任务仍在处理并准备打印,则取消不起作用,并且无论如何都会打印响应。更具体地说,即使单击按钮后,此函数
prompt_mistral("Testing")
仍然会执行并打印响应。
我的代码:
import ollama
import asyncio
import threading
from typing import Optional
import tkinter as tk
# Create the main window
root = tk.Tk()
root.title("Tkinter Button Example")
worker_loop: Optional[asyncio.AbstractEventLoop] = None
task_future: Optional[asyncio.Future] = None
async def get_answer_from_phi3():
print("Trying")
messages = [
{"role": "system", "content": "Hello"}
]
client = ollama.AsyncClient()
stream = await client.chat(
model='phi3',
messages=messages,
stream=True,
options= {
"top_k": 1})
try:
async for chunk in stream:
# Store generated answer
print(chunk['message']['content'], end='', flush=True)
except asyncio.exceptions.CancelledError as e:
print("Cancelled")
pass
except Exception as e:
print(e)
return "Sorry,vv an error occurred while processing your request."
async def prompt_mistral(query):
messages = []
messages.append({"role": "assistant", "content": "Write a song that celebrates the beauty, diversity, and importance of our planet, Earth. The song should evoke vivid imagery of the natural world, from lush forests and majestic mountains to serene oceans and vast deserts. It should capture the essence of Earth as a living, breathing entity that sustains all forms of life. Incorporate themes of harmony, unity, and interconnectedness, emphasizing how all elements of nature are intertwined and how humanity is an integral part of this complex web. The lyrics should reflect a sense of wonder and appreciation for the planet's resources and ecosystems, highlighting the delicate balance that sustains life. Include references to various landscapes, climates, and wildlife, painting a picture of Earth's diverse environments. The song should also touch on the responsibility we have to protect and preserve the planet for future generations, addressing issues like climate change, deforestation, pollution, and conservation efforts. Use poetic language and metaphors to convey the grandeur and fragility of Earth, and infuse the song with a hopeful and inspiring tone that encourages listeners to take action in safeguarding our shared home. The melody should be uplifting and emotionally resonant, complementing the powerful message of the lyrics"})
generated_answer = ''
try:
client = ollama.AsyncClient()
stream = await client.chat(
model='mistral',
messages=messages,
stream=True,
options= {
"top_k": 1}
)
async for chunk in stream:
# Store generated answer
generated_answer += chunk['message']['content']
print(chunk['message']['content'])
except asyncio.exceptions.CancelledError as e:
print("Cancelled reponse")
return
except Exception as e:
print(e)
return "Sorry,vv an error occurred while processing your request."
def prompt_llama(message):
async def prompt():
messages = []
messages.append({"role": "assistant", "content": message})
try:
client = ollama.AsyncClient()
stream = await client.chat(
model='llama2',
messages=messages,
stream=True,
options= {
"top_k": 1}
)
generated_answer = ''
async for chunk in stream:
# Store generated answer
generated_answer += chunk['message']['content']
print(chunk['message']['content'])
if "help" in generated_answer:
await prompt_mistral("Testing")
else:
print(generated_answer)
except asyncio.exceptions.CancelledError as e:
print("Cancelled")
return
except Exception as e:
print(e)
return "Sorry,vv an error occurred while processing your request."
def mistral_worker_function():
global worker_loop, task_future
worker_loop = asyncio.new_event_loop()
task_future = worker_loop.create_task(prompt())
worker_loop.run_until_complete(task_future)
print("Starting thread")
thread = threading.Thread(target=mistral_worker_function)
thread.start()
client = ollama.AsyncClient()
# Define the function to be called when the button is pressed
def on_button_click():
global worker_loop, task_future
# the loop and the future are not threadsafe
worker_loop.call_soon_threadsafe(
lambda: task_future.cancel()
)
def phi3_worker_function():
global worker_loop, task_future
worker_loop = asyncio.new_event_loop()
task_future = worker_loop.create_task(get_answer_from_phi3())
worker_loop.run_until_complete(task_future)
print("Starting thread")
thread = threading.Thread(target=phi3_worker_function())
thread.start()
# Create the button
button = tk.Button(root, text="Stop", command=on_button_click)
# Place the button on the window
button.pack(pady=20)
prompt_llama("Hi")
# Start the Tkinter event loop
root.mainloop()
当您运行自己的异步循环时,您无法从 Tkinter 界面运行任何内容,因此我们的想法是将工作分成两个进程。并使用队列共享关闭信号。您还可以通过应用程序和 ollama 之间的第二个或第三个队列共享消息。
import ollama
from ollama import AsyncClient
import asyncio
import threading
from typing import Optional
import tkinter as tk
from multiprocessing import Process, Manager, Queue
import signal
async def generate_answer(messages):
try:
client = ollama.AsyncClient()
stream = client.chat(
model='llama3',
messages=messages,
stream=True,
options= {
"top_k": 1}
)
async for part in await stream:
print(part['message']['content'], end='', flush=True)
except asyncio.CancelledError:
print("\nCanceled - generator")
except:
print("\nError when executing the task")
finally:
signal.raise_signal(signal.SIGINT)
async def wait_for_cancel(qq: Queue):
# check if job is canceled
while qq.empty():
try:
await asyncio.sleep(0.1)
except asyncio.CancelledError:
print("\nCanceled - waiter")
# coroutine was interrupted
return
t = qq.get_nowait()
print("\nQ state:", t)
signal.raise_signal(signal.SIGINT)
def cancel_tasks():
# cancel all tasks in event loop
tasks = asyncio.all_tasks()
for task in tasks:
if task.cancelled():
continue
try:
task.cancel()
except:
pass
def prompt_llama(message, qq: Queue):
worker_loop = asyncio.get_event_loop()
messages = []
messages.append({"role": "assistant", "content": message})
coro = generate_answer(messages)
task_chat = worker_loop.create_task(coro, name='chat')
coro = wait_for_cancel(qq)
task_wait = worker_loop.create_task(coro, name='waiter')
worker_loop.add_signal_handler(signal.SIGINT, cancel_tasks)
tasks = asyncio.gather(task_chat, task_wait)
worker_loop.run_until_complete(tasks)
def on_button_click(qq):
""" Add to queue information that button was pressed. """
qq.put("cancel")
def create_app(qq):
# Create the main window
print(qq)
app = tk.Tk()
app.title("Tkinter Button Example")
button = tk.Button(app, text="Stop", command=lambda: on_button_click(qq))
button.pack(pady=20)
app.mainloop()
def call_llama(qq):
prompt_llama("Hi", qq)
if __name__ == "__main__":
qq = Queue()
# first process controls Tkinter app
# if cancel is pressed the information is sent via Queue
proc1 = Process(target=create_app, args=(qq, ))
# second process control's Ollama
proc2 = Process(target=call_llama, args=(qq, ))
proc1.start()
proc2.start()
proc1.join()
proc2.join()