[GH-ISSUE #9141] Error calling the local model using langchain, you must use the local proxy to call the local model #5946

Closed
opened 2026-04-12 17:17:30 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @trash-fish on GitHub (Feb 15, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/9141

What is the issue?

Error calling the local model using langchain, you must use the local proxy to call the local model

If I don't add the proxy, my error is as follows:

Traceback (most recent call last):
File "W:\代码\LLM\LangChainDemo01\demo03.py", line 54, in
print(chain.invoke({'content': '你好?'}))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\runnables\base.py", line 3024, in invoke
input = context.run(step.invoke, input, config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 284, in invoke
self.generate_prompt(
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 860, in generate_prompt
return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 690, in generate
self._generate_with_cache(
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 925, in _generate_with_cache
result = self._generate(
^^^^^^^^^^^^^^^
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 701, in _generate
final_chunk = self._chat_stream_with_aggregation(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 602, in _chat_stream_with_aggregation
for stream_resp in self._create_chat_stream(messages, stop, **kwargs):
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 589, in _create_chat_stream
yield from self._client.chat(**chat_params)
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\ollama_client.py", line 168, in inner
raise ResponseError(e.response.text, e.response.status_code) from None
ollama._types.ResponseError: (status code: 502)

mycode:


# 设置代理
# os.environ["http_proxy"] = "http://127.0.0.1:11434"
# os.environ["https_proxy"] = "http://127.0.0.1:11434"



# 调用大语言模型
model = ChatOllama(
    model="llama3.2:1b",
    base_url="http://localhost:11434",
    temperature=0.8
)


# 定义提示模板
prompt = ChatPromptTemplate.from_messages(
    [('system', '你作为ai注释,为用户解决问题'), ('human', '{content}')]
)


# 定义解析器
parser = StrOutputParser()



# 获取链,模板、模型、消息解析器
chain = prompt | model | parser




print(chain.invoke({'content': '你好?'}))

Image

Relevant log output

Traceback (most recent call last):
File "W:\代码\LLM\LangChainDemo01\demo03.py", line 54, in
print(chain.invoke({'content': '你好?'}))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\runnables\base.py", line 3024, in invoke
input = context.run(step.invoke, input, config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 284, in invoke
self.generate_prompt(
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 860, in generate_prompt
return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 690, in generate
self._generate_with_cache(
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 925, in _generate_with_cache
result = self._generate(
^^^^^^^^^^^^^^^
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 701, in _generate
final_chunk = self._chat_stream_with_aggregation(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 602, in _chat_stream_with_aggregation
for stream_resp in self._create_chat_stream(messages, stop, **kwargs):
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 589, in _create_chat_stream
yield from self._client.chat(**chat_params)
File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\ollama_client.py", line 168, in inner
raise ResponseError(e.response.text, e.response.status_code) from None
ollama._types.ResponseError: (status code: 502)

OS

Windows

GPU

Nvidia

CPU

Intel

Ollama version

ollama version is 0.5.10

Originally created by @trash-fish on GitHub (Feb 15, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/9141 ### What is the issue? Error calling the local model using langchain, you must use the local proxy to call the local model If I don't add the proxy, my error is as follows: ``` Traceback (most recent call last): File "W:\代码\LLM\LangChainDemo01\demo03.py", line 54, in print(chain.invoke({'content': '你好?'})) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\runnables\base.py", line 3024, in invoke input = context.run(step.invoke, input, config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 284, in invoke self.generate_prompt( File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 860, in generate_prompt return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 690, in generate self._generate_with_cache( File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 925, in _generate_with_cache result = self._generate( ^^^^^^^^^^^^^^^ File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 701, in _generate final_chunk = self._chat_stream_with_aggregation( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 602, in _chat_stream_with_aggregation for stream_resp in self._create_chat_stream(messages, stop, **kwargs): File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 589, in _create_chat_stream yield from self._client.chat(**chat_params) File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\ollama_client.py", line 168, in inner raise ResponseError(e.response.text, e.response.status_code) from None ollama._types.ResponseError: (status code: 502) ``` mycode: ``` # 设置代理 # os.environ["http_proxy"] = "http://127.0.0.1:11434" # os.environ["https_proxy"] = "http://127.0.0.1:11434" # 调用大语言模型 model = ChatOllama( model="llama3.2:1b", base_url="http://localhost:11434", temperature=0.8 ) # 定义提示模板 prompt = ChatPromptTemplate.from_messages( [('system', '你作为ai注释,为用户解决问题'), ('human', '{content}')] ) # 定义解析器 parser = StrOutputParser() # 获取链,模板、模型、消息解析器 chain = prompt | model | parser print(chain.invoke({'content': '你好?'})) ``` ![Image](https://github.com/user-attachments/assets/2304a24d-6a19-45f0-b8b7-c7be3904309e) ### Relevant log output ```shell Traceback (most recent call last): File "W:\代码\LLM\LangChainDemo01\demo03.py", line 54, in print(chain.invoke({'content': '你好?'})) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\runnables\base.py", line 3024, in invoke input = context.run(step.invoke, input, config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 284, in invoke self.generate_prompt( File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 860, in generate_prompt return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 690, in generate self._generate_with_cache( File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_core\language_models\chat_models.py", line 925, in _generate_with_cache result = self._generate( ^^^^^^^^^^^^^^^ File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 701, in _generate final_chunk = self._chat_stream_with_aggregation( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 602, in _chat_stream_with_aggregation for stream_resp in self._create_chat_stream(messages, stop, **kwargs): File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\langchain_ollama\chat_models.py", line 589, in _create_chat_stream yield from self._client.chat(**chat_params) File "C:\Users\20262\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\ollama_client.py", line 168, in inner raise ResponseError(e.response.text, e.response.status_code) from None ollama._types.ResponseError: (status code: 502) ``` ### OS Windows ### GPU Nvidia ### CPU Intel ### Ollama version ollama version is 0.5.10
GiteaMirror added the bugneeds more info labels 2026-04-12 17:17:30 -05:00
Author
Owner

@trash-fish commented on GitHub (Feb 15, 2025):

If using a proxy allows normal access, but this causes a conflict that prevents my application from connecting to the internet.

os.environ["http_proxy"] = "http://127.0.0.1:11434"
os.environ["https_proxy"] = "http://127.0.0.1:11434"
<!-- gh-comment-id:2661027590 --> @trash-fish commented on GitHub (Feb 15, 2025): If using a proxy allows normal access, but this causes a conflict that prevents my application from connecting to the internet. ``` os.environ["http_proxy"] = "http://127.0.0.1:11434" os.environ["https_proxy"] = "http://127.0.0.1:11434" ```
Author
Owner

@pdevine commented on GitHub (Feb 16, 2025):

This is covered in the FAQ. Can you unset http_proxy and try again?

<!-- gh-comment-id:2661188540 --> @pdevine commented on GitHub (Feb 16, 2025): This is covered in the [FAQ](https://github.com/ollama/ollama/blob/main/docs/faq.md#how-do-i-use-ollama-behind-a-proxy). Can you unset `http_proxy` and try again?
Author
Owner

@trash-fish commented on GitHub (Feb 16, 2025):

This is covered in the FAQ. Can you unset http_proxy and try again?

unset http_proxy This can be solved,thank you

It can also be like this

os.environ["no_proxy"] = "127.0.0.1,localhost"
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"
<!-- gh-comment-id:2661222322 --> @trash-fish commented on GitHub (Feb 16, 2025): > This is covered in the [FAQ](https://github.com/ollama/ollama/blob/main/docs/faq.md#how-do-i-use-ollama-behind-a-proxy). Can you unset `http_proxy` and try again? unset http_proxy This can be solved,thank you It can also be like this ``` os.environ["no_proxy"] = "127.0.0.1,localhost" os.environ["http_proxy"] = "http://127.0.0.1:7890" os.environ["https_proxy"] = "http://127.0.0.1:7890" ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#5946