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

feat: 支持自定义claude的base_url #1172

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
"spark_appid": "", // 你的 讯飞星火大模型 API AppID,用于讯飞星火大模型对话模型
"spark_api_key": "", // 你的 讯飞星火大模型 API Key,用于讯飞星火大模型对话模型
"spark_api_secret": "", // 你的 讯飞星火大模型 API Secret,用于讯飞星火大模型对话模型

"claude_api_host": "", // 你的 Claude Host,用于 Claude 对话模型
"claude_api_secret":"",// 你的 Claude API Secret,用于 Claude 对话模型

"ernie_api_key": "",// 你的文心一言在百度云中的API Key,用于文心一言对话模型
"ernie_secret_key": "",// 你的文心一言在百度云中的Secret Key,用于文心一言对话模型
"ollama_host": "", // 你的 Ollama Host,用于 Ollama 对话模型
Expand Down
2 changes: 2 additions & 0 deletions modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ def load_config_to_environ(key_list):

claude_api_secret = config.get("claude_api_secret", "")
os.environ["CLAUDE_API_SECRET"] = claude_api_secret
claude_api_host = config.get("claude_api_host", "")
os.environ["CLAUDE_API_HOST"] = claude_api_host

ernie_api_key = config.get("ernie_api_key", "")
os.environ["ERNIE_APIKEY"] = ernie_api_key
Expand Down
5 changes: 4 additions & 1 deletion modules/models/Claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@


class Claude_Client(BaseLLMModel):
def __init__(self, model_name, api_secret) -> None:
def __init__(self, model_name, api_secret, api_host) -> None:
super().__init__(model_name=model_name)
self.api_secret = api_secret
self.api_host = api_host
if None in [self.api_secret]:
raise Exception("请在配置文件或者环境变量中设置Claude的API Secret")
self.claude_client = Anthropic(api_key=self.api_secret, base_url=self.api_host)
Expand Down Expand Up @@ -81,11 +82,13 @@ def get_answer_stream_iter(self):
messages=history,
system=system_prompt,
) as stream:
logging.info("Claude流正常输出")
partial_text = ""
for text in stream.text_stream:
partial_text += text
yield partial_text
except Exception as e:
logging.error(f"Claude流异常,异常为: {e}")
yield i18n(GENERAL_ERROR_MSG) + ": " + str(e)

def get_answer_at_once(self):
Expand Down
2 changes: 1 addition & 1 deletion modules/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def get_model(
"SPARK_API_KEY"), os.getenv("SPARK_API_SECRET"), user_name=user_name)
elif model_type == ModelType.Claude:
from .Claude import Claude_Client
model = Claude_Client(model_name=model_name, api_secret=os.getenv("CLAUDE_API_SECRET"))
model = Claude_Client(model_name=model_name, api_secret=os.getenv("CLAUDE_API_SECRET"), api_host=os.getenv("CLAUDE_API_HOST"))
elif model_type == ModelType.Qwen:
from .Qwen import Qwen_Client
model = Qwen_Client(model_name, user_name=user_name)
Expand Down
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
gradio==4.26.0
gradio_client==0.15.1
gradio==4.29.0
gradio_client==0.16.1
primp==0.5.5
pypinyin
tiktoken
Expand Down Expand Up @@ -42,3 +42,5 @@ protobuf==3.20.3
ollama>=0.1.6
numexpr
regex
python-multipart==0.0.9
fastapi==0.112.4