Walkthrough - Using Groq LLM API for free (for scripts or in ComfyUI)

详情

模型描述

本文简要介绍了如何免费获取并使用 Groq LLM API,以及如何在 ComfyUI 中使用它。

文章版本请见:https://civitai.com/articles/4997

无需下载。这不是一个模型,只是下面的文本内容,以及指向文章的链接。

创建账户

此处 创建你的 Groq 账户。这是完全免费的,你可以免费使用 API,但会有一些速率限制,包括每分钟、每天的调用次数和可使用的令牌数量。目前的限制非常宽松。

截至本文撰写时间(2024-04-19),速率限制如下:

创建 API 密钥

前往 GroqCloud Playground,在左侧菜单中找到 API 密钥 部分。


从弹出的对话框中复制 API 密钥。这是你唯一一次能看到这个密钥的机会,请确保保存备份,以便以后使用。

其实无所谓,因为你可以随时创建新密钥并切换使用。

在线测试 API

前往 GroqCloud Playground,在左侧字段中输入系统消息,现在你已准备好测试自定义聊天模型。

User 字段中输入你的聊天消息,然后按 CTRL + ENTER 发送。

获取 Python 代码

在 Playground 页面底部,应有一个“View code”按钮。它会显示后台运行查询和指令的 Python 脚本。

这将弹出一个窗口,显示当前代码:

from groq import Groq

client = Groq()
completion = client.chat.completions.create(
    model="mixtral-8x7b-32768",
    messages=[
        {
            "role": "system",
            "content": "You are an expert python programmer and your mission is to help the user with their programming troubles."
        },
        {
            "role": "user",
            "content": "I need help to write a script. Please write a hello world script for me."
        },
        {
            "role": "assistant",
            "content": "Sure, I'd be happy to help you write a \"Hello, World\" script in Python! Here's an example:\n```python\nprint(\"Hello, World!\")\n```\nWhen you run this script, it will display the message \"Hello, World!\" in the console. Let me know if you have any questions or if there's anything else I can help you with!"
        }
    ],
    temperature=1,
    max_tokens=1024,
    top_p=1,
    stream=True,
    stop=None,
)

for chunk in completion:
    print(chunk.choices[0].delta.content or "", end="")

如果你删除回复内容,实际上就得到了一个可以在本地用 Python 运行、向 API 发送查询的有效代码片段。删除后,代码如下:

from groq import Groq

client = Groq()
completion = client.chat.completions.create(
    model="mixtral-8x7b-32768",
    messages=[
        {
            "role": "system",
            "content": "You are an expert python programmer and your mission is to help the user with their programming troubles."
        },
        {
            "role": "user",
            "content": "I need help to write a script. Please write a hello world script for me."
        }
    ],
    temperature=1,
    max_tokens=1024,
    top_p=1,
    stream=True,
    stop=None,
)

for chunk in completion:
    print(chunk.choices[0].delta.content or "", end="")

将你的 API 密钥添加到脚本中

现在你唯一要做的就是将你的 API 密钥添加到脚本中。修改 client = Groq() 这一行:

client = Groq(api_key="gsk_xxxxxxxxxxxxxxxxxxxxxxxxxx")

现在在 Python 中运行此脚本应该可以正常工作。

请注意,你需要先安装 groq 库。可以运行 pip install groq 来完成。理想情况下,你应该在运行上述操作前创建一个独立的环境,但这属于另一个话题。你可以访问我写的工具来帮助你创建虚拟环境。或者参考网上任何关于如何为 Python 设置虚拟环境的指南。

附加说明:不建议像这样将 API 密钥硬编码到代码中。更好的做法是将其作为环境变量存储在系统中,或使用 configparser 库创建一个 .ini 文件来保存密钥。ChatGPT 可以帮助你完成这一操作。

在 ComfyUI 中使用 Groq

通过上述方法,我创建了一个可在 ComfyUI 中使用的节点。该节点已发布在我的 GitHub 节点包中:https://github.com/MNeMoNiCuZ/ComfyUI-mnemic-nodes?tab=readme-ov-file#-groq-llm-api-node

你可以使用 ComfyUI Manager 安装该节点包,或手动将仓库克隆到你的 /ComfyUI/custom_nodes/ 目录中。

安装完成后,你需要将 API 密钥填入配置文件(ComfyUI\custom_nodes\ComfyUI-mnemic-nodes\nodes\groq),然后即可正常使用!重启 ComfyUI,找到 Groq LLM API 节点并将其添加到你的工作流中。

更多详细信息请参阅 GitHub 页面

ComfyUI Groq LLM API 截图

以下是一些截图,展示该节点的功能:

欢迎在下方提问。

此模型生成的图像

未找到图像。