OpenAI API 访问

您需要获取 OpenAI API 密钥 才能使用这些过程。使用它们会产生 OpenAI 帐户费用。您可以通过在 apoc.conf 中定义 apoc.openai.key 配置来全局设置 API 密钥。

但是,您也可以使用这些过程来调用与 OpenAI 兼容的 API,因此它们将拥有自己的 API 密钥(或者甚至没有 API 密钥)。请参阅下面的段落 OpenAI 兼容提供程序

以下所有过程都可以具有以下 APOC 配置,即在 apoc.conf 中或通过 docker 环境变量 .Apoc 配置

密钥

描述

默认值

apoc.ml.openai.type

"AZURE"、"HUGGINGFACE"、"OPENAI",指示 API 是 Azure、HuggingFace 还是其他 API

"OPENAI"

apoc.ml.openai.url

OpenAI 端点基本 URL

https://api.openai.com/v1(如果 apoc.ml.openai.type=<AZURE OR HUGGINGFACE>,则为空字符串)

此外,它们可以具有以下配置密钥,作为最后一个参数。如果存在,则它们优先于类似的 APOC 配置。

表 1. 常用配置参数

密钥

描述

apiType

类似于 apoc.ml.openai.type APOC 配置

endpoint

类似于 apoc.ml.openai.url APOC 配置

apiVersion

类似于 apoc.ml.azure.api.version APOC 配置

path

自定义添加到基本 URL(由 endpoint 配置定义)的 URL 部分。默认情况下,分别为 apoc.ml.openai.embeddingapoc.ml.openai.completionapoc.ml.openai.chat 过程的 /embeddings/completions/chat/completions

jsonPath

自定义响应的 JSONPath。对于 apoc.ml.openai.chatapoc.ml.openai.completion 过程,默认值为 $,对于 apoc.ml.openai.embedding 过程,默认值为 $.data

因此,我们可以使用以下过程与 Azure 提供的 Open AI 服务一起使用,指向正确的端点 如文档中所述

也就是说,如果我们想调用类似于 https://my-resource.openai.azure.com/openai/deployments/my-deployment-id/embeddings?api-version=my-api-version` 的端点,例如,通过传递配置参数

    {endpoint: "https://my-resource.openai.azure.com/openai/deployments/my-deployment-id",
        apiVersion: my-api-version,
        apiType: 'AZURE'
}

/embeddings 部分将在后台添加。类似地,如果我们使用 apoc.ml.openai.completion,如果我们想要调用类似 https://my-resource.openai.azure.com/openai/deployments/my-deployment-id/completions?api-version=my-api-version 的端点,例如,我们可以编写与上面相同的配置参数,其中 /completions 部分将被添加。

在使用 apoc.ml.openai.chat 时,使用相同的配置,将添加 url 部分 /chat/completions

或者,我们可以编写这个 apoc.conf

apoc.ml.openai.url=https://my-resource.openai.azure.com/openai/deployments/my-deployment-id
apoc.ml.azure.api.version=my-api-version
apoc.ml.openai.type=AZURE

生成嵌入 API

此过程 apoc.ml.openai.embedding 可以接收文本字符串列表,并为每个字符串返回一行,其中嵌入数据为 1536 元素向量。它使用 /embeddings/create API,该 API 在此处有记录

其他配置传递给 API,使用的默认模型为 text-embedding-ada-002

生成嵌入调用
CALL apoc.ml.openai.embedding(['Some Text'], $apiKey, {}) yield index, text, embedding;
表 2. 生成嵌入响应
索引 文本 嵌入

0

"一些文本"

[-0.0065358975, -7.9563365E-4, …​. -0.010693862, -0.005087272]

表 3. 参数
名称 描述

文本

文本字符串列表

apiKey

OpenAI API 密钥

配置

模型和其他请求参数等条目的可选映射。

我们还可以传递自定义 endpoint: <MyAndPointKey> 条目(它优先于 apoc.ml.openai.url 配置)。<MyAndPointKey> 可以是完整的端点(例如,使用 Azure:https://my-resource.openai.azure.com/openai/deployments/my-deployment-id/chat/completions?api-version=my-api-version),或者带有 %s(例如,使用 Azure:https://my-resource.openai.azure.com/openai/deployments/my-deployment-id/%s?api-version=my-api-version),它最终将分别由 apoc.ml.openai.embeddingapoc.ml.openai.chatapoc.ml.openai.completion 替换为 embeddingschat/completioncompletion

authType: `AUTH_TYPE,可以是 authType: "BEARER"(默认配置),通过标头将 apiKey 作为 Authorization: Bearer $apiKey 传递,或者 authType: "API_KEY" 将 apiKey 作为 api-key: $apiKey 标头条目传递。

表 4. 结果
名称 描述

索引

原始列表中的索引条目

文本

原始列表中的文本行

嵌入

ada-002 模型的 1536 元素浮点嵌入向量

文本完成 API

此过程 apoc.ml.openai.completion 可以继续/完成给定的文本。

它使用 /completions/create API,该 API 在此处有记录

其他配置传递给 API,使用的默认模型为 text-davinci-003

文本完成调用
CALL apoc.ml.openai.completion('What color is the sky? Answer in one word: ', $apiKey, {config}) yield value;
文本完成响应
{ created=1684248202, model="text-davinci-003", id="cmpl-7GqBWwX49yMJljdmnLkWxYettZoOy",
  usage={completion_tokens=2, prompt_tokens=12, total_tokens=14},
  choices=[{finish_reason="stop", index=0, text="Blue", logprobs=null}], object="text_completion"}
表 5. 参数
名称 描述

提示

要完成的文本

apiKey

OpenAI API 密钥

配置

模型、温度和其他请求参数等条目的可选映射

表 6. 结果
名称 描述

来自 OpenAI 的结果条目(包含)

OpenLM API

我们还可以调用 HuggingFace 和 Cohere 的完成 API,类似于 OpenLM 库,如下所示。

对于 HuggingFace API,我们必须定义配置 apiType: 'HUGGINGFACE',因为我们必须转换请求体。

例如

CALL apoc.ml.openai.completion('What color is the sky? Answer in one word: ', $huggingFaceApiKey,
{endpoint: 'https://api-inference.huggingface.co/models/gpt2', apiType: 'HUGGINGFACE', model: 'gpt2', path: ''})

或者,通过使用 Cohere API,我们必须定义 path: ''' 以避免在 URL 中添加 /completions 后缀

CALL apoc.ml.openai.completion('What color is the sky? Answer in one word: ', $cohereApiKey,
{endpoint: 'https://api.cohere.ai/v1/generate', path: '', model: 'command'})

聊天完成 API

此过程 apoc.ml.openai.chat 接收助手和用户之间聊天交换的映射列表(以及可选的系统消息),并返回流程中的下一条消息。

它使用 /chat/create API,该 API 在此处有记录

其他配置传递给 API,使用的默认模型为 gpt-3.5-turbo

聊天完成调用
CALL apoc.ml.openai.chat([
{role:"system", content:"Only answer with a single word"},
{role:"user", content:"What planet do humans live on?"}
],  $apiKey) yield value
聊天完成响应
{created=1684248203, id="chatcmpl-7GqBXZr94avd4fluYDi2fWEz7DIHL",
object="chat.completion", model="gpt-3.5-turbo-0301",
usage={completion_tokens=2, prompt_tokens=26, total_tokens=28},
choices=[{finish_reason="stop", index=0, message={role="assistant", content="Earth."}}]}
表 7. 参数
名称 描述

消息

带有 {role:"assistant|user|system", content:"text} 的指令映射列表

apiKey

OpenAI API 密钥

配置

模型、温度和其他请求参数等条目的可选映射

表 8. 结果
名称 描述

来自 OpenAI 的结果条目(包含 created、id、model、object、usage(tokens)、choices(message、index、finish_reason))

兼容 OpenAI 的提供程序

我们还可以使用这些过程来调用兼容 OpenAI 的 API,方法是定义 endpoint 配置,以及可能定义 modelpathjsonPath 配置。

例如,我们可以调用 Anyscale 端点

CALL apoc.ml.openai.embedding(['Some Text'], $anyScaleApiKey,
{endpoint: 'https://api.endpoints.anyscale.com/v1', model: 'thenlper/gte-large'})

或者通过 LocalAI API(请注意,apiKey 默认情况下为 null

CALL apoc.ml.openai.embedding(['Some Text'], "ignored",
{endpoint: 'http://localhost:8080/v1', model: 'text-embedding-ada-002'})

或者,通过使用 LLMatic 库

CALL apoc.ml.openai.embedding(['Some Text'], "ignored",
{endpoint: 'http://localhost:3000/v1', model: 'thenlper/gte-large'})

此外,我们可以使用 Groq API,例如

CALL apoc.ml.openai.chat([{"role": "user", "content": "Explain the importance of low latency LLMs"}],
    '<apiKey>',
    {endpoint: 'https://api.groq.com/openai/v1', model: 'mixtral-8x7b-32768'})