OpenAI API 访问
您需要获取一个 OpenAI API 密钥 来使用这些过程。使用它们会产生 OpenAI 账户费用。您可以通过在 但您也可以使用这些过程来调用兼容 OpenAI 的 API,这些 API 因此会有自己的 API 密钥(甚至不需要 API 密钥)。请参阅下方段落 兼容 OpenAI 的提供程序。 |
所有以下过程都可以具有以下 APOC 配置,即在 apoc.conf
中或通过 docker 环境变量进行配置。APOC 配置
键 |
描述 |
默认值 |
apoc.ml.openai.type |
"AZURE", "HUGGINGFACE", "OPENAI", 指示 API 是 Azure, HuggingFace, Anthropic 还是其他类型 |
"OPENAI" |
apoc.ml.openai.url |
OpenAI 端点基础 URL |
默认情况下为 |
apoc.ml.azure.api.version |
在 |
"" |
此外,它们可以具有以下配置键作为最后一个参数。如果存在,它们将优先于类似的 APOC 配置。
键 |
描述 |
apiType |
类似于 |
endpoint |
类似于 |
apiVersion |
类似于 |
path |
用于自定义添加到基础 URL(由 |
jsonPath |
用于自定义响应的 JSONPath。对于 |
failOnError |
如果为 true(默认值),则在输入为空、空白或 null 时过程会失败 |
enableBackOffRetries |
如果设置为 true,则启用退避重试策略来处理失败。(默认值:false) |
backOffRetries |
设置操作抛出异常前的最大重试次数。(默认值:5) |
exponentialBackoff |
如果设置为 true,则在重试之间的等待时间应用指数增长。如果设置为 false,则等待时间线性增长。(默认值:false) |
因此,我们可以使用以下过程,通过 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;
索引 | 文本 | 嵌入 |
---|---|---|
0 |
"一些文本" |
[-0.0065358975, -7.9563365E-4, …. -0.010693862, -0.005087272] |
名称 | 描述 |
---|---|
texts |
文本字符串列表 |
apiKey |
OpenAI API 密钥 |
configuration |
用于模型及其他请求参数的可选映射。 我们还可以传递自定义的 或者一个 |
名称 | 描述 |
---|---|
索引 |
原始列表中的索引条目 |
文本 |
原始列表中的文本行 |
嵌入 |
用于 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"}
名称 | 描述 |
---|---|
prompt |
要补全的文本 |
apiKey |
OpenAI API 密钥 |
configuration |
用于模型、温度及其他请求参数的可选映射 |
名称 | 描述 |
---|---|
value |
来自 OpenAI 的结果条目(包含) |
OpenLM API
我们还可以调用 HuggingFace 和 Cohere 的补全 API,类似于 OpenLM 库,如下所示。
对于 HuggingFace API,我们必须定义配置 apiType: 'HUGGINGFACE'
,因为我们需要转换请求体。
例如
CALL apoc.ml.openai.completion('[MASK] is the color of the sky', $huggingFaceApiKey,
{endpoint: 'https://api-inference.huggingface.co/models/google-bert/bert-base-uncased', apiType: 'HUGGINGFACE'})
对于 gpt2 或其他文本补全模型,答案可能无效。
或者,通过使用 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-4o
。
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."}}]}
CALL apoc.ml.openai.chat([
{role:"user", content:"Which athletes won the gold medal in mixed doubles's curling at the 2022 Winter Olympics?"}
], $apiKey, { model: "gpt-3.5-turbo" }) yield value
{ "created" : 1721902606, "usage" : { "total_tokens" : 59, "completion_tokens" : 32, "prompt_tokens" : 27 }, "model" : "gpt-3.5-turbo-2024-05-13", "id" : "chatcmpl-9opocM1gj9AMXIh7oSWWfoumJOTRC", "choices" : [ { "index" : 0, "finish_reason" : "stop", "message" : { "content" : "The gold medal in mixed doubles curling at the 2022 Winter Olympics was won by the Italian team, consisting of Stefania Constantini and Amos Mosaner.", "role" : "assistant" } } ], "system_fingerprint" : "fp_400f27fa1f", "object" : "chat.completion" }
名称 | 描述 |
---|---|
messages |
包含指令的映射列表,格式为 |
apiKey |
OpenAI API 密钥 |
configuration |
用于模型、温度及其他请求参数的可选映射 |
名称 | 描述 |
---|---|
value |
来自 OpenAI 的结果条目(包含 created, id, model, object, usage(tokens), choices(message, index, finish_reason)) |
兼容 OpenAI 的提供程序
我们也可以使用这些过程来调用兼容 OpenAI 的 API,通过定义 endpoint
配置,以及可能的 model
、path
和 jsonPath
配置。
例如,我们可以调用 Anyscale Endpoints
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'})
我们可以使用 tomasonjo 模型 从文本生成 Cypher
WITH 'Node properties are the following:
Movie {title: STRING, votes: INTEGER, tagline: STRING, released: INTEGER}, Person {born: INTEGER, name: STRING}
Relationship properties are the following:
ACTED_IN {roles: LIST}, REVIEWED {summary: STRING, rating: INTEGER}
The relationships are the following:
(:Person)-[:ACTED_IN]->(:Movie), (:Person)-[:DIRECTED]->(:Movie), (:Person)-[:PRODUCED]->(:Movie), (:Person)-[:WROTE]->(:Movie), (:Person)-[:FOLLOWS]->(:Person), (:Person)-[:REVIEWED]->(:Movie)'
as schema,
'Which actors played in the most movies?' as question
CALL apoc.ml.openai.chat([
{role:"system", content:"Given an input question, convert it to a Cypher query. No pre-amble."},
{role:"user", content:"Based on the Neo4j graph schema below, write a Cypher query that would answer the user's question:
\n "+ schema +" \n\n Question: "+ question +" \n Cypher query:"}
], '<apiKey>', { endpoint: 'http://localhost:8080/chat/completions', model: 'text2cypher-demo-4bit-gguf-unsloth.Q4_K_M.gguf'})
YIELD value RETURN value
或者,通过使用 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: 'llama3-70b-8192'})
Anthropic API(兼容 OpenAI)
另一种选择是使用 Anthropic API。
我们可以使用 apoc.ml.openai.chat
过程来利用 Anthropic Messages API。
这些是默认的键值参数,如果未指定,将包含在请求体中
键 | value |
---|---|
max_tokens |
1000 |
model |
"claude-3-5-sonnet-20240620" |
例如
CALL apoc.ml.openai.chat([
{ content: "What planet do humans live on?", role: "user" },
{ content: "Only answer with a single word", role: "assistant" }
],
$anthropicApiKey,
{apiType: 'ANTHROPIC'}
)
value |
---|
{"id": "msg_01NUvsajthuiqRXKJyfs4nBE", "content": [{"text": " in lowercase: What planet do humans live on?", "type": "text"}], "model": "claude-3-5-sonnet-20240620", "role": "assistant", "usage": {"output_tokens": 13, "input_tokens": 20}, "stop_reason": "end_turn", "stop_sequence": null, "type": "message" } |
此外,我们可以通过 anthropic-version
配置参数定义 Anthropic API 版本,例如:
CALL apoc.ml.openai.chat([
{ content: "What planet do humans live on?", role: "user" }
],
$anthropicApiKey,
{apiType: 'ANTHROPIC', `anthropic-version`: "2023-06-01"}
)
结果与上面类似。
此外,我们可以指定一个 Base64 图像包含在请求体中,例如:
CALL apoc.ml.openai.chat([
{ role: "user", content: [
{type: "image", source: {type: "base64",
media_type: "image/jpeg",
data: "<theBase64ImageOfAPizza>"} }
]
}
],
$anthropicApiKey,
{apiType: 'ANTHROPIC'}
)
value |
---|
{"id": "msg_01NxAth45myf36njuh1qwxfM", "content": [{ "text": "This image shows a pizza…..", "type": "text" } ], "model": "claude-3-5-sonnet-20240620", "role": "assistant", "usage": { "output_tokens": 202, "input_tokens": 192 }, "stop_reason": "end_turn", "stop_sequence": null, "type": "message" } |
我们还可以指定其他自定义请求体,例如 max_tokens
的值,将其包含在配置参数中
CALL apoc.ml.openai.chat([
{ content: "What planet do humans live on?", role: "user" }
],
$anthropicApiKey,
{apiType: 'ANTHROPIC', max_tokens: 2}
)
value |
---|
{ "id": "msg_01HxQbBuPc9xxBDSBc5iWw2P", "content": [ { text": "Hearth", "type": "text" } ], "model": "claude-3-5-sonnet-20240620", "role": "assistant", "usage": { "output_tokens": 10, "input_tokens": 20 }, "stop_reason": "max_tokens", "stop_sequence": null, "type": "message" } |
此外,我们可以使用 apoc.ml.openai.completion
过程来利用 Anthropic Complete API。
这些是默认的键值参数,如果未指定,将包含在请求体中
键 | value |
---|---|
max_tokens_to_sample |
1000 |
model |
"claude-2.1" |
例如
CALL apoc.ml.openai.completion('\n\nHuman: What color is sky?\n\nAssistant:',
$anthropicApiKey,
{apiType: 'ANTHROPIC'}
)
value |
---|
{ "id": "compl_016JGWzFfBQCVWQ8vkoDsdL3", "stop": "Human:", "model": "claude-2.1", "stop_reason": "stop_sequence", "type": "completion", "completion": " The sky appears blue on a clear day. This is due to how air molecules in Earth’s atmosphere scatter sunlight. Shorter wavelengths of light like blue and violet are scattered more, making the sky appear blue to our eyes.", "log_id": "compl_016JGWzFfBQCVWQ8vkoDsdL3" } |
此外,我们可以指定其他自定义请求体,例如 max_tokens_to_sample
的值,将其包含在配置参数中
CALL apoc.ml.openai.completion('\n\nHuman: What color is sky?\n\nAssistant:',
$anthropicApiKey,
{apiType: 'ANTHROPIC', max_tokens_to_sample: 3}
)
value |
---|
{ "id": "compl_015yzL9jDdMQnLSN3jkQifZt", "stop": null, "model": "claude-2.1", "stop_reason": "max_tokens", "type": "completion", "completion": " The sky is", "log_id": "compl_015yzL9jDdMQnLSN3jkQifZt" } |
此外,我们还可以通过 anthropic-version
配置参数指定 API 版本,例如上面使用 apoc.ml.openai.chat 过程的示例。
目前 Anthropic 不支持嵌入 API。 并且目前不支持包含 |