Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue May 22, 2024 · 2 comments
Labels
component:python sdk Issue/PR related to Python SDK status:triaged Issue/PR triaged to the corresponding sub-team type:bug Something isn't working

Comments

@dreamJune
Copy link

dreamJune commented May 22, 2024

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 dreamJune added component:python sdk Issue/PR related to Python SDK type:bug Something isn't working labels May 22, 2024
@singhniraj08 singhniraj08 added the status:triaged Issue/PR triaged to the corresponding sub-team label May 23, 2024
@dreamJune dreamJune changed the title 使用代理服务器地址创建chat_session.sendmessage抛出异常: 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')) 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) May 23, 2024
@dreamJune
Copy link
Author

According to issue: #117, I have tried the code,but it does not work
genai.configure(api_key=GOOGLE_API_KEY, transport='rest')

@dreamJune
Copy link
Author

dreamJune commented Jun 4, 2024

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.
google-gemini/cookbook#73 (comment)
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')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:python sdk Issue/PR related to Python SDK status:triaged Issue/PR triaged to the corresponding sub-team type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants