1 Star 0 Fork 0

yuyangup/ComfyUI-IF_AI_tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
groq_api.py 1.78 KB
一键复制 编辑 原始数据 按行查看 历史
ImpactFrames 提交于 2024-06-10 01:29 . Add files via upload
from groq import Groq
import json
def send_groq_request(model, system_message, user_message,
messages, api_key, temperature, max_tokens,
base64_image=None):
try:
client = Groq(api_key=api_key)
data = {
"model": model,
"messages": prepare_groq_messages(base64_image, system_message, user_message, messages),
"temperature": temperature,
"max_tokens": max_tokens
}
response = client.chat.completions.create(**data)
if response.choices:
choice = response.choices[0]
message = choice.message
try:
assistant_content = json.loads(message.content)[0].get('content')
except json.JSONDecodeError:
assistant_content = message.content
assistant_content = assistant_content.replace("\n\n", " ").strip()
print(assistant_content)
return assistant_content
else:
print("No valid choices in the response.")
print("Full response:", response)
return "No valid response generated.", messages
except Exception as e:
return f"Error: {str(e)}", messages
def prepare_groq_messages(base64_image, system_message, user_message, messages):
# Add the user's message to the history
messages.append({"role": "user", "content": user_message})
groq_messages = [{"role": "system", "content": system_message}]
for message in messages:
if message["role"] != "system":
# Removed str conversion
groq_messages.append(message)
if base64_image:
groq_messages[1]["image"] = base64_image
return groq_messages
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuyangup/ComfyUI-IF_AI_tools.git
git@gitee.com:yuyangup/ComfyUI-IF_AI_tools.git
yuyangup
ComfyUI-IF_AI_tools
ComfyUI-IF_AI_tools
IF_AI_tools_RAG_linux_mac

搜索帮助

0d507c66 1850385 C8b1a773 1850385