Spaces:
Running
Running
Update prompt_engineer/call_llm.py
Browse files- 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
|
| 5 |
-
import
|
| 6 |
|
| 7 |
import streamlit as st
|
| 8 |
import pandas as pd
|
| 9 |
-
import
|
| 10 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
client = OpenAI(
|
| 40 |
api_key=api_key,
|
| 41 |
-
base_url=config
|
| 42 |
)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
model=config["model_name"],
|
| 47 |
messages=[
|
| 48 |
{"role": "system", "content": system_msg},
|
| 49 |
{"role": "user", "content": prompt},
|
| 50 |
],
|
| 51 |
-
stream
|
| 52 |
)
|
| 53 |
-
return
|
| 54 |
|
| 55 |
except OpenAIError as e:
|
| 56 |
-
|
| 57 |
-
st.error(
|
| 58 |
-
|
| 59 |
-
return "调用失败,请检查密钥或网络"
|
| 60 |
except Exception as e:
|
| 61 |
-
|
| 62 |
-
st.error(
|
| 63 |
-
return
|
| 64 |
-
|
| 65 |
-
elif
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 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"
|
| 104 |
|
| 105 |
except Exception as e:
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 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
|
|
|
|
|
|
|
| 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
|