MetaGPT icon indicating copy to clipboard operation
MetaGPT copied to clipboard

Issue with Proxy Configuration in config2.yaml Not Working in MetaGPT v0.7.3--在MetaGPT v0.7.3中使用config2.yaml配置代理无效,但通过os包设置环境变量可以

Open ming188199 opened this issue 1 year ago • 4 comments
trafficstars

Bug description

I'm currently using MetaGPT version 0.7.3 and encountering an issue with proxy configuration. When I attempt to configure the proxy settings directly in the config2.yaml file, it seems to have no effect, and I'm unable to connect to the OpenAI API through the proxy. However, when I configure the proxy settings using the os package in Python to set environment variables, everything works as expected, and I can successfully make requests to the OpenAI API.

Here's a brief overview of what I've tried and the results:

  1. Using config2.yaml for Proxy Configuration (Not Working):
    • config2.yaml:
# Full Example: https://github.com/geekan/MetaGPT/blob/main/config/config2.example.yaml
# Reflected Code: https://github.com/geekan/MetaGPT/blob/main/metagpt/config2.py
llm:
  api_type: "openai"  # or azure / ollama / open_llm etc. Check LLMType for more options
  model: "gpt-3.5-turbo-1106"  # or gpt-3.5-turbo-1106 / gpt-4-1106-preview
  base_url: "https://api.openai-forward.com/v1"  # or forward url / other llm url
  api_key: "sk-t5y***UxRs"
proxy: 'http://127.0.0.1:20171'  # Optional. If you want to use a proxy, set it here.
  • I added proxy settings in config2.yaml, but it seems like these settings are ignored. I encountered the following error message, indicating a timeout issue:
    2024-03-04 22:16:52.369 | ERROR    | metagpt.provider.openai_api:log_and_reraise:43 - Retry attempts exhausted. Last exception: Request timed out.
    2024-03-04 22:16:52.370 | WARNING  | metagpt.provider.openai_api:log_and_reraise:44 - 
    Recommend going to https://deepwisdom.feishu.cn/wiki/MsGnwQBjiif9c3koSJNcYaoSnu4#part-XdatdVlhEojeAfxaaEZcMV3ZniQ
    See FAQ 5.8
    
  1. Using os Package for Proxy Configuration (Working):
    • By setting the http_proxy and https_proxy environment variables using the os package in Python, I'm able to successfully connect and make requests to the OpenAI API. The environment variable settings are as follows:
import os

os.environ["http_proxy"] = "http://127.0.0.1:20171"
os.environ["https_proxy"] = "http://127.0.0.1:20171"

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file
#set up API key and other OpenAI settings
os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY_7days')
os.environ["OPENAI_API_MODEL"] = "gpt-3.5-turbo" # 选择你要使用的模型
os.environ["OPENAI_API_BASE"] = "https://api.openai-forward.com/v1"

I'm wondering if there's a known issue with proxy configuration in config2.yaml in MetaGPT v0.7.3 or if there's a specific format or step I'm missing for the configuration to be recognized.

Bug solved method

Environment information

  • LLM type and model name:
  • System version: Mac OS 14.1.2
  • Python version: 3.11
  • packages version: 0.7.3
  • installation method:

Screenshots or logs

中文版本

标题: 在MetaGPT v0.7.3中使用config2.yaml配置代理无效,但通过os包设置环境变量可以

正文: 你好,

我目前正在使用MetaGPT版本0.7.3,并遇到了一个关于代理配置的问题。当我尝试在config2.yaml文件中直接配置代理设置时,似乎没有任何效果,我无法通过代理连接到OpenAI API。然而,当我使用Python中的os包来设置环境变量配置代理设置时,一切都按预期工作,我可以成功地向OpenAI API发出请求。

以下是我尝试的简要概述及结果:

  1. 使用config2.yaml进行代理配置(不起作用):

    • 我在config2.yaml中添加了代理设置,但似乎这些设置被忽略了。我遇到了以下错误消息,指示有一个超时问题:
      2024-03-04 22:16:52.369 | ERROR    | metagpt.provider.openai_api:log_and_reraise:43 - 重试尝试已耗尽。最后的异常:请求超时。
      2024-03-04 22:16:52.370 | WARNING  | metagpt.provider.openai_api:log_and_reraise:44 - 
      建议访问 https://deepwisdom.feishu.cn/wiki/MsGnwQBjiif9c3koSJNcYaoSnu4#part-XdatdVlhEojeAfxaaEZcMV3ZniQ
      参见FAQ 5.8
      
  2. 使用os包进行代理配置(起作用):

    • 通过在Python中使用os包设置http_proxyhttps_proxy环境变量,我能够成功连接并向OpenAI API发出请求。环境变量设置如下:
      # 设置API密钥和其他OpenAI设置
      os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY_7days')
      os.environ["OPENAI_API_MODEL"] = "gpt-3.5-turbo" # 选择你要使用的模型
      os.environ["OPENAI_API_BASE"] = "https://api.openai-forward.com/v1"
      

我想知道在MetaGPT v0.7.3中使用config2.yaml进行代理配置是否存在已知问题,或者是否有我错过的特定格式或步骤,以使配置被识别。

非常感谢您能就此事提供任何帮助或指导。

谢谢!

ming188199 avatar Mar 05 '24 01:03 ming188199

There is no configuration example for proxy in config2.yaml.example. The correct config2.yaml configuration example is as follows:

llm:
  api_type: "openai"  # or azure / ollama etc.
  base_url: "YOUR_BASE_URL"
  api_key: "YOUR_API_KEY"
  model: "gpt-4-turbo-preview"  # or gpt-3.5-turbo-1106 / gpt-4-1106-preview
  proxy: "YOUR_LLM_PROXY"

iorisa avatar Mar 05 '24 12:03 iorisa

It's ok the openai type, but gemini happen that:

2024-03-07 09:35:19.222 | ERROR    | metagpt.utils.common:log_it:476 - Finished call to 'metagpt.actions.action_node.ActionNode._aask_v1' after 367.430(s), this was the 6th time calling it. exp: Timeout of 60.0s exceeded, last exception: 503 failed to connect to all addresses; last error: UNAVAILABLE: ipv4:142.251.42.234:443: Failed to connect to remote host: FD shutdown
2024-03-07 09:35:19.228 | WARNING  | metagpt.utils.common:wrapper:571 - There is a exception in role's execution, in order to resume, we delete the newest role communication message in the role's memory.
2024-03-07 09:35:19.264 | ERROR    | metagpt.utils.common:wrapper:553 - Exception occurs, start to serialize the project, exp:
Traceback (most recent call last):
  File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/google/api_core/grpc_helpers_async.py", line 77, in wait_for_connection
    await self._call.wait_for_connection()
  File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/grpc/aio/_call.py", line 650, in wait_for_connection
    await self._raise_for_status()
grpc.aio._call.AioRpcError: <AioRpcError of RPC that terminated with:
        status = StatusCode.UNAVAILABLE
        details = "failed to connect to all addresses; last error: UNAVAILABLE: ipv4:142.251.42.234:443: Failed to connect to remote host: FD shutdown"
        debug_error_string = "UNKNOWN:Error received from peer  {created_time:"2024-03-07T09:35:19.218562+08:00", grpc_status:14, grpc_message:"failed to connect to all addresses; last error: UNAVAILABLE: ipv4:142.251.42.234:443: Failed to connect to remote host: FD shutdown"}"
>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/google/api_core/retry/retry_unary_async.py", line 155, in retry_target
    return await target()
           ^^^^^^^^^^^^^^
  File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/google/api_core/grpc_helpers_async.py", line 189, in error_remapped_callable
    await call.wait_for_connection()
google.api_core.exceptions.ServiceUnavailable: 503 failed to connect to all addresses; last error: UNAVAILABLE: ipv4:142.251.42.234:443: Failed to connect to remote host: FD shutdown

the openai llm like below :

llm:
  api_type: "openai"  # or azure / ollama etc.
  base_url: "YOUR_BASE_URL"
  api_key: "YOUR_API_KEY"
  model: "gpt-4-turbo-preview"  # or gpt-3.5-turbo-1106 / gpt-4-1106-preview
  proxy: "YOUR_LLM_PROXY"

the gemini llm like below :

llm:
  api_type: "gemini"  # 默认支持模型gemini-pro
  api_key: "AIza**RWQE"
  proxy: "http://127.0.0.1:20171"

ming188199 avatar Mar 07 '24 01:03 ming188199

fixbug: https://github.com/geekan/MetaGPT/pull/971/files

iorisa avatar Mar 07 '24 04:03 iorisa

It still not working on google_gemini_api, but I can run openAI API. Environment information

LLM type and model name: System version: Mac OS 14.1.2 Python version: 3.11 packages version: 0.7.6

config2.yaml

llm:
  api_type: "gemini"  # 默认支持模型gemini-pro
  api_key: "AIzaSyD*QE"
  proxy: "http://127.0.0.1:20171"

It seems fixed and tell me use proxy http://127.0.0.1:20171 2024-03-22 22:45:28.597 | INFO | metagpt.provider.google_gemini_api:__init_gemini:62 - Use proxy: http://127.0.0.1:20171 2024-03-22 22:45:28.598 | INFO | main:main:99 - write a function that calculates the product of a list and run it 2024-03-22 22:45:28.599 | INFO | main:_act:83 - Alice(RunnableCoder): to do SimpleWriteCode(SimpleWriteCode) 2024-03-22 22:46:27.301 | WARNING | metagpt.utils.common:wrapper:572 - There is a exception in role's execution, in order to resume, we delete the newest role communication message in the role's memory. Traceback (most recent call last): File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/google/api_core/grpc_helpers_async.py", line 77, in wait_for_connection await self._call.wait_for_connection() File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/grpc/aio/_call.py", line 650, in wait_for_connection await self._raise_for_status() File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/grpc/aio/_call.py", line 263, in _raise_for_status raise _create_rpc_error( grpc.aio._call.AioRpcError: <AioRpcError of RPC that terminated with: status = StatusCode.UNAVAILABLE details = "failed to connect to all addresses; last error: UNKNOWN: ipv4:142.251.43.10:443: tcp handshaker shutdown" debug_error_string = "UNKNOWN:Error received from peer {grpc_message:"failed to connect to all addresses; last error: UNKNOWN: ipv4:142.251.43.10:443: tcp handshaker shutdown", grpc_status:14, created_time:"2024-03-22T22:46:27.299019+08:00"}"

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/google/api_core/retry/retry_unary_async.py", line 155, in retry_target return await target() ^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/google/api_core/grpc_helpers_async.py", line 189, in error_remapped_callable await call.wait_for_connection() File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/google/api_core/grpc_helpers_async.py", line 79, in wait_for_connection raise exceptions.from_grpc_error(rpc_error) from rpc_error google.api_core.exceptions.ServiceUnavailable: 503 failed to connect to all addresses; last error: UNKNOWN: ipv4:142.251.43.10:443: tcp handshaker shutdown

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/metagpt/utils/common.py", line 563, in wrapper return await func(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/metagpt/roles/role.py", line 558, in run rsp = await self.react() ^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/metagpt/roles/role.py", line 527, in react rsp = await self._act_by_order() ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/metagpt/roles/role.py", line 481, in _act_by_order rsp = await self._act() ^^^^^^^^^^^^^^^^^ File "/Users/kk/Documents/github/AGI/MetaGPT/test.py", line 89, in _act result = await todo.run(msg.content) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/Documents/github/AGI/MetaGPT/test.py", line 30, in run rsp = await self._aask(prompt) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/metagpt/actions/action.py", line 93, in _aask return await self.llm.aask(prompt, system_msgs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/metagpt/provider/base_llm.py", line 89, in aask rsp = await self.acompletion_text(message, stream=stream, timeout=timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/tenacity/_asyncio.py", line 88, in async_wrapped return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/tenacity/_asyncio.py", line 47, in call do = self.iter(retry_state=retry_state) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/tenacity/init.py", line 314, in iter return fut.result() ^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/concurrent/futures/_base.py", line 449, in result return self.__get_result() ^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result raise self._exception File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/tenacity/_asyncio.py", line 50, in call result = await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/metagpt/provider/google_gemini_api.py", line 147, in acompletion_text return await self._achat_completion_stream(messages) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/metagpt/provider/google_gemini_api.py", line 122, in _achat_completion_stream resp: AsyncGenerateContentResponse = await self.llm.generate_content_async( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/google/generativeai/generative_models.py", line 272, in generate_content_async iterator = await self._async_client.stream_generate_content(request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/google/api_core/retry/retry_unary_async.py", line 230, in retry_wrapped_func return await retry_target( ^^^^^^^^^^^^^^^^^^^ File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/google/api_core/retry/retry_unary_async.py", line 160, in retry_target _retry_error_helper( File "/Users/kk/opt/anaconda3/envs/MetaGPT/lib/python3.11/site-packages/google/api_core/retry/retry_base.py", line 221, in _retry_error_helper raise final_exc from source_exc google.api_core.exceptions.RetryError: Timeout of 60.0s exceeded, last exception: 503 failed to connect to all addresses; last error: UNKNOWN: ipv4:142.251.43.10:443: tcp handshaker shutdown

ming188199 avatar Mar 22 '24 15:03 ming188199

@ming188199 Can you try it again?

geekan avatar Mar 23 '24 10:03 geekan

My fault... Key typo. Try out v0.7.7

iorisa avatar Mar 23 '24 10:03 iorisa

@ming188199 Can you try it again?

I don't know how to test. I am waiting to update 0.7.7 Uploading 截屏2024-03-23 22.35.09.png…

ming188199 avatar Mar 23 '24 14:03 ming188199