Elffuss BraidHub

kikocis/auto-code-rover public

Fix bug: connection issue to ollama from inside container
Changesetb0e6ebbd983f4da2c33fc14c4f56ba310a73eb0219b503ac92c35a76f51dd818
AuthorZhang Yuntong <ang.unong@gmail.com> · 2024-05-07 13:50 +08:00
CommitterZhang Yuntong <ang.unong@gmail.com> · 2024-05-07 13:54 +08:00
Parents0f4e2e4e2ecb

Browse the files at this changeset

2 files changed

modified README.md +10 −3
@@ -134,8 +134,8 @@
134134
135135 If patch generation is successful, the path to the generated patch will be printed in the end.
136136
137-Web UI is also provided for visualization of the issue fixing process.
138-In the docker shell, run the following command:
137+Web UI is also provided for visualization of the issue fixing process.
138+In the docker shell, run the following command:
139139
140140 ```bash
141141 cd /opt/auto-code-rover/demo_vis/
@@ -259,7 +259,14 @@
259259 | Meta | Llama 3 70B | --model llama3:70b |
260260 | | Llama 3 8B | --model llama3 |
261261
262-Before using the llama models, please [install ollama](https://ollama.com/download/linux) and download the corresponding models with ollama (e.g. `ollama pull llama3`).
262+> [!NOTE]
263+> Some notes on running ACR with local models such as llama3:
264+> 1. Before using the llama3 models, please [install ollama](https://ollama.com/download/linux) and download the corresponding models with ollama (e.g. `ollama pull llama3`).
265+> 2. You can run ollama server on the host machine, and ACR in its container. ACR will attempt to communicate to the ollama server on host.
266+> 3. If your setup is ollama in host + ACR in its container, we recommend installing [Docker Desktop](https://docs.docker.com/desktop/) on the host, in addition to the [Docker Engine](https://docs.docker.com/engine/).
267+> - Docker Desktop contains Docker Engine, and also has a virtual machine which makes it easier to access the host ports from within a container. With Docker Desktop, this setup will work without additional effort.
268+> - When the docker installation is only Docker Engine, you may need to add either `--net=host` or `--add-host host.docker.internal=host-gateway` to the `docker run` command when starting the ACR container, so that ACR can communicate with the ollama server on the host machine.
269+
263270
264271 ## Experiment Replication
265272
modified app/model/ollama.py +30 −3
@@ -8,6 +8,7 @@
88 from copy import deepcopy
99 from typing import Literal, cast
1010
11+import httpx
1112 import ollama
1213 import timeout_decorator
1314 from ollama._types import Message, Options
@@ -35,6 +36,7 @@
3536 return
3637 # local models are free
3738 super().__init__(name, 0.0, 0.0)
39+ self.client: ollama.Client | None = None
3840 self._initialized = True
3941
4042 def setup(self) -> None:
@@ -51,6 +53,9 @@
5153 e,
5254 )
5355 sys.exit(1)
56+ except Exception as e:
57+ print("Could not communicate with ollama server due to exception.", e)
58+ sys.exit(1)
5459
5560 @timeout_decorator.timeout(120) # 2 min
5661 def send_empty_request(self):
@@ -60,7 +65,27 @@
6065 (2) preload the model for faster response time (models will be kept in memory for 5 mins after loaded)
6166 (see https://github.com/ollama/ollama/blob/main/docs/faq.md#how-can-i-pre-load-a-model-to-get-faster-response-times)
6267 """
63- ollama.chat(model=self.name, messages=[])
68+ # localhost is used when (1) running both ACR and ollama on host machine; and
69+ # (2) running ollama in host, and ACR in container with --net=host
70+ local_client = ollama.Client(host="http://localhost:11434")
71+ # docker_host_client is used when running ollama in host and ACR in container, and
72+ # Docker Desktop is installed
73+ docker_host_client = ollama.Client(host="http://host.docker.internal:11434")
74+ try:
75+ local_client.chat(model=self.name, messages=[])
76+ self.client = local_client
77+ return
78+ except httpx.ConnectError:
79+ # failed to connect to client at localhost
80+ pass
81+
82+ try:
83+ docker_host_client.chat(model=self.name, messages=[])
84+ self.client = docker_host_client
85+ except httpx.ConnectError:
86+ # also failed to connect via host.docker.internal
87+ print("Could not connect to ollama server.")
88+ sys.exit(1)
6489
6590 def check_api_key(self) -> str:
6691 return "No key required for local models."
@@ -90,6 +115,8 @@
90115 json_stop_words.append("```")
91116 json_stop_words.append(" " * 10)
92117 # FIXME: ignore tools field since we don't use tools now
118+
119+ assert self.client is not None
93120 try:
94121 # build up options for ollama
95122 options = {
@@ -105,7 +132,7 @@
105132 messages.append(json_instruction)
106133 # give more stop words and lower max_token for json mode
107134 options.update({"stop": json_stop_words, "num_predict": 128})
108- response = ollama.chat(
135+ response = self.client.chat(
109136 model=self.name,
110137 messages=cast(list[Message], messages),
111138 format="json",
@@ -114,7 +141,7 @@
114141 )
115142 else:
116143 options.update({"stop": stop_words, "num_predict": 1024})
117- response = ollama.chat(
144+ response = self.client.chat(
118145 model=self.name,
119146 messages=cast(list[Message], messages),
120147 options=cast(Options, options),