google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.19k stars 227 forks source link

chat_session.send_message throw exception while using proxy config: raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPSConnectionPool(host='generativelanguage.googleapis.com', port=443) #356

Open dreamJune opened 1 month ago

dreamJune commented 1 month ago

Description of the bug:

how can i call the gemini service with proxy service.When I call Gemini's upload file and create cha t session service through the local proxy service, an exception is thrown,i had checked the curl and proxy service ,they both works.here is the exception: raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPSConnectionPool(host='generativelanguage.googleapis.com', port=443): Max retries exceeded with url: /v1beta/models/gemini-1.5-pro-latest:generateContent?%24alt=json%3Benum-encoding%3Dint (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x110fde060>: Failed to establish a new connection: Socket error: [Errno 54] Connection reset by peer'))

here is the code:,python version 3.12.2,google-generativeai version 0.5.4:

import os import google.generativeai as genai from dotenv import load_dotenv import socket import socks

socks.set_default_proxy(socks.PROXY_TYPE_HTTP, "127.0.0.1", 10000) socket.socket = socks.socksocket load_dotenv() os.environ["HTTP_PROXY"] = "http://127.0.0.1:9999" os.environ["HTTP_PROXYS"] = "http://127.0.0.1:9999"

genai.configure(api_key=gemini_key,transport='rest')

generation_config = { "temperature": 1, "top_p": 0.95, "top_k": 64, "max_output_tokens": 8192, "response_mime_type": "text/plain", } safety_settings = [ { "category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE", }, { "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE", }, { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE", }, { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE", }, ]

model = genai.GenerativeModel( model_name="gemini-1.5-pro-latest", safety_settings=safety_settings, generation_config=generation_config, system_instruction="system instrution")

chat_session = model.start_chat( history=[ ] )

response = chat_session.send_message("INSERT_INPUT_HERE")

print(response.text) print(chat_session.history)`

Actual vs expected behavior:

No response

Any other information you'd like to share?

No response

dreamJune commented 1 month ago

According to issue: https://github.com/google-gemini/generative-ai-python/issues/117, I have tried the code,but it does not work genai.configure(api_key=GOOGLE_API_KEY, transport='rest')

dreamJune commented 1 month ago

thanks for ,i get the fixed code from another issue.but i still prefer to have a proxy config rather than to set the os enviroment. https://github.com/google-gemini/cookbook/issues/73#issuecomment-2067676391 The error happens in the network connection even you change the VPN node. You can set python code as below: os.environ['http_proxy']='http://127.0.0.1:7890/' os.environ['https_proxy']='http://127.0.0.1:7890/' os.environ['all_proxy']='socks5://127.0.0.1:7890' genai.configure(api_key=GOOGLE_API_KEY,transport='rest')