Skip to content

Commit 776a7f8

Browse files
committed
feat: added ability to use ollama running remotely
Refs: #171
1 parent 5ee9bdf commit 776a7f8

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,9 +1,11 @@
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
from prompt_toolkit.shortcuts import input_dialog
6-
import google.generativeai as genai
79

810

911
class Config:
@@ -14,6 +16,7 @@ class Config:
1416
verbose (bool): Flag indicating whether verbose mode is enabled.
1517
openai_api_key (str): API key for OpenAI.
1618
google_api_key (str): API key for Google.
19+
ollama_host (str): url to ollama running remotely.
1720
"""
1821

1922
_instance = None
@@ -33,6 +36,9 @@ def __init__(self):
3336
self.google_api_key = (
3437
None # instance variables are backups in case saving to a `.env` fails
3538
)
39+
self.ollama_host = (
40+
None # instance variables are backups in case savint to a `.env` fails
41+
)
3642

3743
def initialize_openai(self):
3844
if self.verbose:
@@ -71,6 +77,19 @@ def initialize_google(self):
7177
model = genai.GenerativeModel("gemini-pro-vision")
7278

7379
return model
80+
81+
def initialize_ollama(self):
82+
if self.ollama_host:
83+
if self.verbose:
84+
print("[Config][initialize_ollama] using cached ollama host")
85+
else:
86+
if self.verbose:
87+
print(
88+
"[Config][initialize_ollama] no cached ollama host. Assuming ollama running locally."
89+
)
90+
self.ollama_host = os.getenv("OLLAMA_HOST", None)
91+
model = Client(host=self.ollama_host)
92+
return model
7493

7594
def validation(self, model, voice_mode):
7695
"""

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()
@@ -456,6 +451,7 @@ def call_ollama_llava(messages):
456451
print("[call_ollama_llava]")
457452
time.sleep(1)
458453
try:
454+
model = config.initialize_ollama()
459455
screenshots_dir = "screenshots"
460456
if not os.path.exists(screenshots_dir):
461457
os.makedirs(screenshots_dir)
@@ -482,7 +478,7 @@ def call_ollama_llava(messages):
482478
}
483479
messages.append(vision_message)
484480

485-
response = ollama.chat(
481+
response = model.chat(
486482
model="llava",
487483
messages=messages,
488484
)

0 commit comments

Comments
 (0)