Skip to content

Commit 547ea38

Browse files
iazraelyihong0618
andauthored
gemini 支持传入自定义域名 (#485)
* gemini 支持传入自定义域名 * 还原误改的readme格式 * fix: lint Signed-off-by: yihong0618 <[email protected]> --------- Signed-off-by: yihong0618 <[email protected]> Co-authored-by: yihong <[email protected]>
1 parent 5c7c9ec commit 547ea38

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ xiaogpt --hardware LX06 --mute_xiaoai --use_chatgpt_api
7474
xiaogpt --hardware LX06 --mute_xiaoai --stream
7575
# 如果你想使用 google 的 gemini
7676
xiaogpt --hardware LX06 --mute_xiaoai --use_gemini --gemini_key ${gemini_key}
77+
# 如果你想使用自己的 google gemini 服务
78+
python3 xiaogpt.py --hardware LX06 --mute_xiaoai --use_gemini --gemini_key ${gemini_key} --gemini_api_domain ${gemini_api_domain}
7779
# 如果你想使用阿里的通义千问
7880
xiaogpt --hardware LX06 --mute_xiaoai --use_qwen --qen_key ${qwen_key}
7981
# 如果你想用 edge-tts
@@ -101,6 +103,8 @@ python3 xiaogpt.py --hardware LX06 --mute_xiaoai --stream
101103
python3 xiaogpt.py --hardware LX06 --mute_xiaoai --use_glm --glm_key ${glm_key}
102104
# 如果你想使用 google 的 gemini
103105
python3 xiaogpt.py --hardware LX06 --mute_xiaoai --use_gemini --gemini_key ${gemini_key}
106+
# 如果你想使用自己的 google gemini 服务
107+
python3 xiaogpt.py --hardware LX06 --mute_xiaoai --use_gemini --gemini_key ${gemini_key} --gemini_api_domain ${gemini_api_domain}
104108
# 如果你想使用阿里的通义千问
105109
python3 xiaogpt.py --hardware LX06 --mute_xiaoai --use_qwen --qen_key ${qwen_key}
106110
# 如果你想使用 LangChain+SerpApi 实现上网检索或其他本地服务(目前仅支持 stream 模式)
@@ -155,6 +159,7 @@ ChatGLM [文档](http://open.bigmodel.cn/doc/api#chatglm_130b)
155159
| serpapi_api_key | serpapi的key 参考 [SerpAPI](https://serpapi.com/) | | |
156160
| glm_key | chatglm 的 apikey | | |
157161
| gemini_key | gemini 的 apikey [参考](https://makersuite.google.com/app/apikey) | | |
162+
| gemini_api_domain | gemini 的自定义域名 [参考](https://github.com/antergone/palm-netlify-proxy) | |
158163
| qwen_key | qwen 的 apikey [参考](https://help.aliyun.com/zh/dashscope/developer-reference/api-details) | | |
159164
| cookie | 小爱账户cookie (如果用上面密码登录可以不填) | | |
160165
| mi_did | 设备did | | |
@@ -175,11 +180,9 @@ ChatGLM [文档](http://open.bigmodel.cn/doc/api#chatglm_130b)
175180
| bing_cookie_path | NewBing使用的cookie路径,参考[这里]获取 | 也可通过环境变量 `COOKIE_FILE` 设置 | |
176181
| bing_cookies | NewBing使用的cookie字典,参考[这里]获取 | | |
177182
| deployment_id | Azure OpenAI 服务的 deployment ID | 参考这个[如何找到deployment_id](https://github.com/yihong0618/xiaogpt/issues/347#issuecomment-1784410784) | |
178-
| api_base | 如果需要替换默认的api,或者使用Azure OpenAI 服务 | 例如:`https://abc-def.openai.azure.com/` | |
183+
| api_base | 如果需要替换默认的api,或者使用Azure OpenAI 服务 | 例如:`https://abc-def.openai.azure.com/` |
179184
| volc_access_key | 火山引擎的 access key 请在[这里](https://console.volcengine.com/iam/keymanage/)获取 | | |
180185
| volc_secret_key | 火山引擎的 secret key 请在[这里](https://console.volcengine.com/iam/keymanage/)获取 | | |
181-
182-
183186
[这里]: https://github.com/acheong08/EdgeGPT#getting-authentication-required
184187

185188
## 注意

xiao_config.json.example

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"openai_key": "",
66
"glm_key": "",
77
"gemini_key": "",
8+
"gemini_api_domain": "",
89
"qwen_key": "",
910
"serpapi_api_key": "",
1011
"cookie": "",

xiaogpt/bot/gemini_bot.py

+24-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"temperature": 0.7,
1313
"top_p": 1,
1414
"top_k": 1,
15-
"max_output_tokens": 2048,
15+
"max_output_tokens": 4096,
1616
}
1717

1818
safety_settings = [
@@ -32,10 +32,26 @@
3232
class GeminiBot(ChatHistoryMixin, BaseBot):
3333
name = "Gemini"
3434

35-
def __init__(self, gemini_key: str) -> None:
35+
def __init__(self, gemini_key: str, gemini_api_domain: str) -> None:
3636
import google.generativeai as genai
3737

38-
genai.configure(api_key=gemini_key)
38+
from google.auth import api_key
39+
40+
credentials = api_key.Credentials(gemini_key)
41+
if len(gemini_api_domain) > 0:
42+
print("Use custom gemini_api_domain: " + gemini_api_domain)
43+
credentials._universe_domain = gemini_api_domain
44+
genai.configure(
45+
transport="rest",
46+
credentials=credentials,
47+
client_options={
48+
"api_endpoint": "https://" + gemini_api_domain,
49+
"universe_domain": gemini_api_domain,
50+
},
51+
)
52+
else:
53+
genai.configure(api_key=gemini_key)
54+
3955
self.history = []
4056
model = genai.GenerativeModel(
4157
model_name="gemini-pro",
@@ -46,11 +62,13 @@ def __init__(self, gemini_key: str) -> None:
4662

4763
@classmethod
4864
def from_config(cls, config):
49-
return cls(gemini_key=config.gemini_key)
65+
return cls(
66+
gemini_key=config.gemini_key, gemini_api_domain=config.gemini_api_domain
67+
)
5068

5169
async def ask(self, query, **options):
52-
self.convo.send_message(query)
53-
message = self.convo.last.text.strip()
70+
response = self.convo.send_message(query)
71+
message = response.text.strip()
5472
print(message)
5573
if len(self.convo.history) > 10:
5674
self.convo.history = self.convo.history[2:]

xiaogpt/cli.py

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ def main():
3737
dest="gemini_key",
3838
help="gemini api key",
3939
)
40+
parser.add_argument(
41+
"--gemini_api_domain",
42+
dest="gemini_api_domain",
43+
help="custom gemini api domain",
44+
)
4045
parser.add_argument(
4146
"--qwen_key",
4247
dest="qwen_key",

xiaogpt/config.py

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class Config:
5656
gemini_key: str = os.getenv("GEMINI_KEY", "") # keep the old rule
5757
qwen_key: str = os.getenv("DASHSCOPE_API_KEY", "") # keep the old rule
5858
serpapi_api_key: str = os.getenv("SERPAPI_API_KEY", "")
59+
gemini_api_domain: str = os.getenv(
60+
"GEMINI_API_DOMAIN", ""
61+
) # 自行部署的 Google Gemini 代理
5962
volc_access_key: str = os.getenv("VOLC_ACCESS_KEY", "")
6063
volc_secret_key: str = os.getenv("VOLC_SECRET_KEY", "")
6164
proxy: str | None = None

0 commit comments

Comments
 (0)