Skip to content

Commit 0154496

Browse files
committed
Local II Preview
1 parent 887b702 commit 0154496

File tree

16 files changed

+921
-1727
lines changed

16 files changed

+921
-1727
lines changed

interpreter/core/computer/vision/vision.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self, computer):
1919
self.tokenizer = None # Will load upon first use
2020

2121
def load(self):
22-
print("\n *Loading Moondream model...*\n")
22+
print("\nLoading Moondream (vision)...\n")
2323
try:
2424
with contextlib.redirect_stdout(
2525
open(os.devnull, "w")

interpreter/core/llm/llm.py

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import litellm
55

66
litellm.suppress_debug_info = True
7+
import subprocess
78
import time
89
import uuid
910

@@ -133,10 +134,14 @@ def run(self, messages):
133134
precursor = "Imagine I have just shown you an image with this description: "
134135
postcursor = ""
135136

137+
image_description = self.vision_renderer(lmc=img_msg)
138+
139+
# It would be nice to format this as a message to the user and display it like: "I see: image_description"
140+
136141
img_msg["content"] = (
137-
# precursor
138-
# + self.vision_renderer(lmc=img_msg) +
139-
"\n---\nThe image contains the following text exactly: '''\n"
142+
precursor
143+
+ image_description
144+
+ "\n---\nThe image contains the following text exactly, which may or may not be relevant (if it's not relevant, ignore this): '''\n"
140145
+ self.interpreter.computer.vision.ocr(lmc=img_msg)
141146
+ "\n'''"
142147
+ postcursor
@@ -195,9 +200,9 @@ def run(self, messages):
195200
"""
196201
**We were unable to determine the context window of this model.** Defaulting to 3000.
197202
198-
If your model can handle more, run `interpreter.llm.context_window = {token limit}`.
203+
If your model can handle more, run `self.context_window = {token limit}`.
199204
200-
Also please set `interpreter.llm.max_tokens = {max tokens per response}`.
205+
Also please set `self.max_tokens = {max tokens per response}`.
201206
202207
Continuing...
203208
"""
@@ -259,6 +264,46 @@ def run(self, messages):
259264
else:
260265
yield from run_text_llm(self, params)
261266

267+
def load(self):
268+
if self.model.startswith("ollama/"):
269+
# WOAH we should also hit up ollama and set max_tokens and context_window based on the LLM. I think they let u do that
270+
271+
model_name = self.model.replace("ollama/", "")
272+
try:
273+
# List out all downloaded ollama models. Will fail if ollama isn't installed
274+
result = subprocess.run(
275+
["ollama", "list"], capture_output=True, text=True, check=True
276+
)
277+
except Exception as e:
278+
print(str(e))
279+
self.interpreter.display_message(
280+
f"> Ollama not found\n\nPlease download Ollama from [ollama.com](https://ollama.com/) to use `{model_name}`.\n"
281+
)
282+
exit()
283+
284+
lines = result.stdout.split("\n")
285+
names = [
286+
line.split()[0].replace(":latest", "")
287+
for line in lines[1:]
288+
if line.strip()
289+
] # Extract names, trim out ":latest", skip header
290+
291+
if model_name not in names:
292+
self.interpreter.display_message(f"\nDownloading {model_name}...\n")
293+
subprocess.run(["ollama", "pull", model_name], check=True)
294+
295+
# Send a ping, which will actually load the model
296+
print(f"\nLoading {model_name}...\n")
297+
298+
old_max_tokens = self.max_tokens
299+
self.max_tokens = 1
300+
self.interpreter.computer.ai.chat("ping")
301+
self.max_tokens = old_max_tokens
302+
303+
# self.interpreter.display_message("\n*Model loaded.*\n")
304+
305+
# Validate LLM should be moved here!!
306+
262307

263308
def fixed_litellm_completions(**params):
264309
"""
@@ -289,7 +334,7 @@ def fixed_litellm_completions(**params):
289334

290335
if "api key" in str(first_error).lower() and "api_key" not in params:
291336
print(
292-
"LiteLLM requires an API key. Please set a dummy API key to prevent this message. (e.g `interpreter --api_key x` or `interpreter.llm.api_key = 'x'`)"
337+
"LiteLLM requires an API key. Please set a dummy API key to prevent this message. (e.g `interpreter --api_key x` or `self.api_key = 'x'`)"
293338
)
294339

295340
# So, let's try one more time with a dummy API key:

interpreter/core/respond.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def respond(interpreter):
198198
code = re.sub(r"import computer\.\w+\n", "pass\n", code)
199199
# If it does this it sees the screenshot twice (which is expected jupyter behavior)
200200
if any(
201-
code.split("\n")[-1].startswith(text)
201+
code.strip().split("\n")[-1].startswith(text)
202202
for text in [
203203
"computer.display.view",
204204
"computer.display.screenshot",

0 commit comments

Comments
 (0)