ElvisWang111 commited on
Commit
5e39d02
·
verified ·
1 Parent(s): 4623123

Update prompt_engineer/call_llm.py

Browse files
Files changed (1) hide show
  1. prompt_engineer/call_llm.py +85 -63
prompt_engineer/call_llm.py CHANGED
@@ -1,15 +1,12 @@
1
- import re
2
  from openai import OpenAI, OpenAIError
3
  from anthropic import Anthropic, AnthropicError
4
- import requests
5
- import json
6
 
7
  import streamlit as st
8
  import pandas as pd
9
- import numpy as np
10
- from config import MODEL_CONFIGS
11
- from typing import IO, List, Dict
12
- from zai import ZhipuAiClient
13
 
14
  class LLMClient:
15
  def __init__(self, model_configs: dict, api_keys: dict, model: str):
@@ -34,77 +31,104 @@ class LLMClient:
34
  )
35
 
36
  try:
37
- if model_name == "GPT-4o" or model_name == "GPT-5" or model_name == "DeepSeek" or model_name == "通义千问" or model_name == "Claude" or model_name == "豆包":
 
 
 
 
 
38
  try:
39
  client = OpenAI(
40
  api_key=api_key,
41
- base_url=config["api_base"]
42
  )
43
 
44
- # 使用新的 API 调用方式
45
- resp = client.chat.completions.create(
46
- model=config["model_name"],
47
  messages=[
48
  {"role": "system", "content": system_msg},
49
  {"role": "user", "content": prompt},
50
  ],
51
- stream = False
52
  )
53
- return resp.choices[0].message.content
54
 
55
  except OpenAIError as e:
56
- # 这里可以捕获所有OpenAI SDK定义的错误
57
- st.error(f"API调用失败: {str(e)}")
58
- # 记录日志或提示用户
59
- return "调用失败,请检查密钥或网络"
60
  except Exception as e:
61
- # 捕获其他非预期的异常,如网络问题
62
- st.error(f"发生未知错误: {str(e)}")
63
- return "发生未知错误"
64
-
65
- elif model_name == "智谱AI":
66
- client = ZhipuAiClient(api_key=api_key)
67
- response = client.chat.completions.create(
68
- model=config["model_name"],
69
- messages=[{"role": "system", "content": "你是一个专业的数据分析助手。"},
70
- {"role": "user", "content": prompt}],
71
- thinking={
72
- "type":"enabled"
73
- }
74
- )
75
- if response:
76
- print(response.choices[0].message)
77
- desc = response.choices[0].message.content if hasattr(response.choices[0].message, "content") else str(response.choices[0].message)
78
- return desc.replace("<|begin_of_box|>", "").replace("<|end_of_box|>", "").strip()
 
 
 
 
 
 
 
 
 
 
79
 
80
- st.error(f"智谱调用失败:{response.text}")
81
- return "调用失败,请检查密钥或网络"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
-
84
- # elif model_name == "DeepSeek":
85
- # client = OpenAI(
86
- # api_key=api_key,
87
- # base_url=config["api_base"])
88
-
89
- # resp = client.chat.completions.create(
90
- # model=config["model_name"],
91
- # messages=[
92
- # {"role": "system", "content": system_msg},
93
- # {"role": "user", "content": prompt},
94
- # ],
95
- # stream=False
96
- # )
97
- # if resp:
98
- # return resp.choices[0].message.content
99
- # st.error(f"DeepSeek调用失败:{resp.text}")
100
- # return "调用失败,请检查密钥或网络"
101
 
102
  else:
103
- return f"暂不支持模型:{model_name}"
104
 
105
  except Exception as e:
106
- st.error(f"{model_name} 调用异常:{e}")
107
- return "大模型调用失败,请检查 API 密钥或网络连接"
 
108
 
109
 
110
  def add_memory(self, entry: Dict[str, str]) -> None:
@@ -124,13 +148,11 @@ class LLMClient:
124
 
125
  def add_df(self, input_df) -> None:
126
 
127
-
128
-
129
  self.df = input_df
130
 
131
 
132
  def load_df(self) -> pd.DataFrame:
133
-
134
  return self.df
135
 
136
 
@@ -141,4 +163,4 @@ class LLMClient:
141
 
142
  def has_df(self) -> bool:
143
 
144
- return self.df == None
 
 
1
  from openai import OpenAI, OpenAIError
2
  from anthropic import Anthropic, AnthropicError
3
+ import zai
4
+ from zai import ZhipuAiClient
5
 
6
  import streamlit as st
7
  import pandas as pd
8
+ from typing import List, Dict
9
+
 
 
10
 
11
  class LLMClient:
12
  def __init__(self, model_configs: dict, api_keys: dict, model: str):
 
31
  )
32
 
33
  try:
34
+ # 获取 API 类型
35
+ api_type = config.get("api_type", "openai")
36
+
37
+ # 根据 API 类型选择不同的调用方式
38
+ if api_type == "openai":
39
+ # OpenAI 兼容的 API(包括 OpenAI、DeepSeek、通义千问、豆包等)
40
  try:
41
  client = OpenAI(
42
  api_key=api_key,
43
+ base_url=config.get("api_base", "https://api.openai.com/v1")
44
  )
45
 
46
+ response = client.chat.completions.create(
47
+ model=config.get("model_name", "gpt-4o"),
 
48
  messages=[
49
  {"role": "system", "content": system_msg},
50
  {"role": "user", "content": prompt},
51
  ],
52
+ stream=False
53
  )
54
+ return response.choices[0].message.content
55
 
56
  except OpenAIError as e:
57
+ msg = f"API 调用失败:{str(e)}"
58
+ st.error(msg)
59
+ return f"调用失败,请检查密钥或网络({msg})"
 
60
  except Exception as e:
61
+ msg = f"发生未知错误:{str(e)}"
62
+ st.error(msg)
63
+ return msg
64
+
65
+ elif api_type == "claude":
66
+ # Claude 使用 Anthropic SDK
67
+ try:
68
+ client = Anthropic(api_key=api_key)
69
+
70
+ response = client.messages.create(
71
+ model=config.get("model_name", "claude-3-5-sonnet-latest"),
72
+ max_tokens=4096,
73
+ system=system_msg,
74
+ messages=[
75
+ {"role": "user", "content": prompt}
76
+ ]
77
+ )
78
+
79
+ return response.content[0].text
80
+
81
+ except AnthropicError as e:
82
+ msg = f"Claude API 调用失败:{str(e)}"
83
+ st.error(msg)
84
+ return f"调用失败,请检查密钥或网络({msg})"
85
+ except Exception as e:
86
+ msg = f"发生未知错误:{str(e)}"
87
+ st.error(msg)
88
+ return msg
89
 
90
+ elif api_type == "zhipu":
91
+ # 智谱 AI 使用自己的客户端
92
+ # 参考 https://github.com/zai-org/z-ai-sdk-python
93
+ try:
94
+ client = ZhipuAiClient(
95
+ api_key=api_key,
96
+ base_url=config.get("api_base", "https://open.bigmodel.cn/api/paas/v4")
97
+ )
98
+
99
+ response = client.chat.completions.create(
100
+ model=config.get("model_name", "glm-4v-plus-0111"),
101
+ messages=[
102
+ {"role": "system", "content": system_msg},
103
+ {"role": "user", "content": prompt}
104
+ ],
105
+ thinking={"type": "enabled"}
106
+ )
107
+ desc = response.choices[0].message.content if hasattr(
108
+ response.choices[0].message, "content"
109
+ ) else str(response.choices[0].message)
110
+ return desc.replace("<|begin_of_box|>", "").replace("<|end_of_box|>", "").strip()
111
 
112
+ except zai.core.APIStatusError as e:
113
+ msg = f"智谱 API 状态错误:{str(e)}"
114
+ st.error(msg)
115
+ return f"调用失败,请检查密钥或网络({msg})"
116
+ except zai.core.APITimeoutError as e:
117
+ msg = f"智谱 API 请求超时:{str(e)}"
118
+ st.error(msg)
119
+ return f"调用失败,请检查密钥或网络({msg})"
120
+ except Exception as e:
121
+ msg = f"发生未知错误:{str(e)}"
122
+ st.error(msg)
123
+ return msg
 
 
 
 
 
 
124
 
125
  else:
126
+ return f"不支持的 API 类型:{api_type}"
127
 
128
  except Exception as e:
129
+ msg = f"{model_name} 调用异常:{e}"
130
+ st.error(msg)
131
+ return f"大模型调用失败,请检查 API 密钥或网络连接({msg})"
132
 
133
 
134
  def add_memory(self, entry: Dict[str, str]) -> None:
 
148
 
149
  def add_df(self, input_df) -> None:
150
 
 
 
151
  self.df = input_df
152
 
153
 
154
  def load_df(self) -> pd.DataFrame:
155
+
156
  return self.df
157
 
158
 
 
163
 
164
  def has_df(self) -> bool:
165
 
166
+ return self.df is None