Stackwire Runner for OpenAI

一个用于试验 OpenAI API 提示的命令行应用

目前仅支持聊天补全 API。

设置实验

一个实验将需要三个文件

  • 实验文件 – 包含调用 OpenAI 所需的所有配置信息
  • 提示文件 – 您希望包含在 OpenAI 提示中的文件
  • API 密钥文件 – 包含您的 OpenAI API 密钥

首先创建您的实验文件。您可以为其命名,默认情况下程序将查找“experiment.json”。以下是多次运行提示的示例。

{
 "experiment_name" : "multirun-exp",
 "number_of_runs" : 2,
 "response_format" : "text",
 "output_dir" : "output",
 "api_key_file": "api_key.txt",
 "prompt_template" : "prompt.txt",
 "ai_config" : {
   "model": "gpt-3.5-turbo",
   "temperature": 1.2,
   "top_p": 1,
   "max_tokens": 500
 },
 "template_values" : {
   "rank" : "Commander in Starfleet",
   "show" : "The Original Star Trek"
 }
}

您还可以通过将 `response_format` 设置为“json”并设置 `run_depth` > 0 来链接提示。提示需要以 JSON 格式返回,并且 JSON 字段将用于填充发送到 OpenAI 的下一个提示的值。您可以在“example”目录中查看如何实现这一点。

{
  "experiment_name" : "chain-exp",
  "number_of_runs" : 1,
  "response_format" : "json",
  "run_depth" : 2,
  "output_dir" : "output",
  "api_key_file": "api_key.txt",
  "prompt_template" : "prompt-json.txt",
  "ai_config" : {
    "model": "gpt-3.5-turbo",
    "temperature": 1.2,
    "top_p": 1,
    "max_tokens": 500
  },
  "template_values" : {
    "rank" : "Commander in Starfleet"
  }
}
字段 描述
experiment_name 您的实验名称。请勿使用空格或非法字符。
number_of_runs 运行您的实验的次数。这将是您调用 OpenAI API 的次数。
output_dir 您希望输出文件的目录
api_key_file 您的 API 密钥文件的路径
prompt_template 您的提示模板的路径
template_values 您想填入提示模板的任何值。此字段必须存在,但可以为空。
response_format json 或 text。如果未指定,默认值为 text。
run_depth 提示链的深度。默认值为 0。
ai_config 发送到 OpenAI 的配置参数。您可以添加 OpenAI 使用的所有合法参数。

接下来创建您的提示文件:您可以为其命名,在我们的例子中是 **data/prompt.txt**。下面提供了一个示例。来自实验文件的模板值将替换特殊语法 ${rank}

Write me a story about ${rank}. One Sentence Only.

发送到 OpenAI 的提示将是

Write me a story about Commander in Starfleet. One Sentence Only.

如果您正在进行提示链,请考虑以下示例

Write me a story about ${rank}. The main character is ${mainCharacterName}. If no main character is given, choose one. Write one sentence only.
The response should be in JSON using the following structure. Only use these fields. {"mainCharacterName": "${mainCharacterName}"}

在第一次请求时,发送到 OpenAI 的提示将是

Write me a story about Commander in Starfleet. The main character is . If no main character is given, choose one. Write one sentence only.
The response should be in JSON using the following structure. Only use these fields. {"mainCharacterName": ""}

响应的内容

{"mainCharacterName": "Captain Kirk"}

第二次请求

Write me a story about Commander in Starfleet. The main character is Captain Kirk. If no main character is given, choose one. Write one sentence only.
The response should be in JSON using the following structure. Only use these fields. {"mainCharacterName": "Captain Kirk"}

添加 API 密钥

添加一个包含您的 API 密钥的文件(下面的不是真实的)。在我们的例子中,我们称之为 **api_key.txt**

sk-gKtTxOumv4orO6cfWlh0ZK

下载程序

您可以自己构建此项目。或从 Artifacts 下载适用于您平台的 stackwire 可执行文件。它们在 Actions 选项卡下。下载后解压缩。

https://githubdocs.cn/en/actions/managing-workflow-runs/downloading-workflow-artifacts

对于 macos 和 linux,您需要使文件可执行

chmod a+x stackwire

对于 Mac,在从命令行运行时,您需要执行类似以下的操作。 https://thewiredshopper.com/apple-cannot-check-for-malicious-software-error/

运行实验

运行以下命令。

stackwire experiments.json

上述命令将生成输出目录,其中包含 OpenAI 所有请求和响应的记录。

输出

对于每次运行,您都将获得发送的请求记录

{
  "model": "gpt-3.5-turbo",
  "temperature": 1.2,
  "top_p": 1,
  "max_tokens": 500,
  "messages": [
    {
      "role": "user",
      "content": "Write me a story about Commander in Starfleet. One Sentence Only."
    }
  ]
}

以及来自 OpenAI 的响应。这将允许您确定使用的 token。

{
  "id": "chatcmpl-74WgfENKxOmQwRwtpoJ6IFELyzNTL",
  "object": "chat.completion",
  "created": 1681313317,
  "model": "gpt-3.5-turbo-0301",
  "usage": {
    "prompt_tokens": 22,
    "completion_tokens": 27,
    "total_tokens": 49
  },
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "As Commander of the USS Enterprise, Jean-Luc Picard boldly leads his crew through perilous missions and treks through the galaxy."
      },
      "finish_reason": "stop",
      "index": 0
    }
  ]
}

您还将获得运行的纯文本输出

exp-multirun
Write me a story about Commander in Starfleet. One Sentence Only.
As Commander of the USS Enterprise, Jean-Luc Picard boldly leads his crew through perilous missions and treks through the galaxy.

构建此项目

确保您已安装 dart。然后从项目根目录

dart pub get

dart compile exe bin/stackwire.dart -o stackwire

GitHub

查看 Github