Skip to content

fast_llm: Fix UnboundLocalError, add docstring #1592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion interpreter/core/computer/ai/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,36 @@ def chunk_responses(responses, tokens, llm):


def fast_llm(llm, system_message, user_message):
"""
Creates a temporary chat context to process a single query, then restores the original chat state.

This is used for auxiliary queries (like summarization) that shouldn't affect the main conversation.
Particularly important for local LLMs where creating new instances is expensive.

Args:
llm: The LLM instance to use (typically computer.interpreter.llm)
system_message: The system prompt for this specific query
user_message: The user message/content to process

Returns:
str: The LLM's response content

Note:
This function temporarily replaces the LLM's conversation state (messages and system prompt),
runs the query, then restores the original state. This allows us to run one-off queries
without disrupting the main conversation context.
"""
old_messages = llm.interpreter.messages
old_system_message = llm.interpreter.system_message
try:
llm.interpreter.system_message = system_message
llm.interpreter.messages = []
response = llm.interpreter.chat(user_message)
return response[-1].get("content")
finally:
# Always restore the old state (before returning)
llm.interpreter.messages = old_messages
llm.interpreter.system_message = old_system_message
return response[-1].get("content")


def query_map_chunks(chunks, llm, query):
Expand Down