Skip to content

Commit 6c91917

Browse files
authored
Merge pull request #172 from mjspeck/upstream-main
feat: added ability to use ollama running remotely
2 parents 0a22173 + 991ebe9 commit 6c91917

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

operate/config.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import os
22
import sys
3+
4+
import google.generativeai as genai
35
from dotenv import load_dotenv
6+
from ollama import Client
47
from openai import OpenAI
58
import anthropic
69
from prompt_toolkit.shortcuts import input_dialog
7-
import google.generativeai as genai
810

911

1012
class Config:
@@ -15,6 +17,7 @@ class Config:
1517
verbose (bool): Flag indicating whether verbose mode is enabled.
1618
openai_api_key (str): API key for OpenAI.
1719
google_api_key (str): API key for Google.
20+
ollama_host (str): url to ollama running remotely.
1821
"""
1922

2023
_instance = None
@@ -34,6 +37,9 @@ def __init__(self):
3437
self.google_api_key = (
3538
None # instance variables are backups in case saving to a `.env` fails
3639
)
40+
self.ollama_host = (
41+
None # instance variables are backups in case savint to a `.env` fails
42+
)
3743
self.anthropic_api_key = (
3844
None # instance variables are backups in case saving to a `.env` fails
3945
)
@@ -76,6 +82,19 @@ def initialize_google(self):
7682

7783
return model
7884

85+
def initialize_ollama(self):
86+
if self.ollama_host:
87+
if self.verbose:
88+
print("[Config][initialize_ollama] using cached ollama host")
89+
else:
90+
if self.verbose:
91+
print(
92+
"[Config][initialize_ollama] no cached ollama host. Assuming ollama running locally."
93+
)
94+
self.ollama_host = os.getenv("OLLAMA_HOST", None)
95+
model = Client(host=self.ollama_host)
96+
return model
97+
7998
def initialize_anthropic(self):
8099
if self.anthropic_api_key:
81100
api_key = self.anthropic_api_key

operate/models/apis.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,31 @@
1+
import base64
2+
import io
3+
import json
14
import os
25
import time
3-
import json
4-
import base64
56
import traceback
6-
import io
7+
78
import easyocr
89
import ollama
9-
10+
import pkg_resources
1011
from PIL import Image
1112
from ultralytics import YOLO
1213

1314
from operate.config import Config
1415
from operate.exceptions import ModelNotRecognizedException
15-
from operate.utils.screenshot import (
16-
capture_screen_with_cursor,
17-
)
1816
from operate.models.prompts import (
17+
get_system_prompt,
1918
get_user_first_message_prompt,
2019
get_user_prompt,
21-
get_system_prompt,
2220
)
23-
from operate.utils.ocr import get_text_element, get_text_coordinates
24-
25-
2621
from operate.utils.label import (
2722
add_labels,
2823
get_click_position_in_percent,
2924
get_label_coordinates,
3025
)
31-
from operate.utils.style import ANSI_GREEN, ANSI_RED, ANSI_RESET, ANSI_BRIGHT_MAGENTA
32-
import pkg_resources
33-
26+
from operate.utils.ocr import get_text_coordinates, get_text_element
27+
from operate.utils.screenshot import capture_screen_with_cursor
28+
from operate.utils.style import ANSI_BRIGHT_MAGENTA, ANSI_GREEN, ANSI_RED, ANSI_RESET
3429

3530
# Load configuration
3631
config = Config()
@@ -568,6 +563,7 @@ def call_ollama_llava(messages):
568563
print("[call_ollama_llava]")
569564
time.sleep(1)
570565
try:
566+
model = config.initialize_ollama()
571567
screenshots_dir = "screenshots"
572568
if not os.path.exists(screenshots_dir):
573569
os.makedirs(screenshots_dir)
@@ -594,7 +590,7 @@ def call_ollama_llava(messages):
594590
}
595591
messages.append(vision_message)
596592

597-
response = ollama.chat(
593+
response = model.chat(
598594
model="llava",
599595
messages=messages,
600596
)

0 commit comments

Comments
 (0)