Skip to content

Commit

Permalink
added doc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed Jul 30, 2024
1 parent 5f55ce1 commit 4b2fc02
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 14 deletions.
183 changes: 173 additions & 10 deletions docs/asciidoc/modules/ROOT/pages/ml/openai.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,23 @@ CALL apoc.ml.openai.chat([{"role": "user", "content": "Explain the importance of
{endpoint: 'https://api.groq.com/openai/v1', model: 'mixtral-8x7b-32768'})
----

=== Anthropic API (OpenAI-compatible)

Another alternative is to use the https://docs.anthropic.com/en/api/getting-started[Anthropic API].

We can use the `apoc.ml.openai.chat` procedure to leverage the https://docs.anthropic.com/en/api/messages[Anthropic Messages API], that is:
We can use the `apoc.ml.openai.chat` procedure to leverage the https://docs.anthropic.com/en/api/messages[Anthropic Messages API].

These are the default key-value parameters that will be included in the body request, if not specified:

.Default Anthropic key-value parameters
[%autowidth, opts=header]
|===
| key | value
| max_tokens | 1000
| model | "claude-3-5-sonnet-20240620"
|===

For example:

[source,cypher]
----
Expand All @@ -266,32 +280,181 @@ CALL apoc.ml.openai.chat([
)
----

.Default Anthropic body parameters
.Example result
[%autowidth, opts=header]
|===
|name | description
| max_tokens | 1000
| model | claude-3-5-sonnet-20240620
| 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"
}
|===

Also, we can use the `apoc.ml.openai.completion` procedure to leverage the https://docs.anthropic.com/en/api/complete[Anthropic Complete API]:

Moreover, we can define the Anthropic API Version via the `anthropic-version` config parameter, e.g.:

[source,cypher]
----
CALL apoc.ml.openai.completion('What color is the sky?',
CALL apoc.ml.openai.chat([
{ content: "What planet do humans live on?", role: "user" }
],
$anthropicApiKey,
{apiType: 'ANTHROPIC', `anthropic-version`: "2023-06-01"}
)
----

with a result similar to above.


Additionally, we can specify a Base64 image to include in the body, e.g.:

[source,cypher]
----
CALL apoc.ml.openai.chat([
{ role: "user", content: [
{type: "image", source: {type: "base64",
media_type: "image/jpeg",
data: "<theBase64ImageOfAPizza>"} }
]
}
],
$anthropicApiKey,
{apiType: 'ANTHROPIC'}
)
----

.Example result
[%autowidth, opts=header]
|===
| 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"
}
|===

We can also specify other custom body requests, like the `max_tokens` value, to be included in the config parameter:

[source,cypher]
----
CALL apoc.ml.openai.chat([
{ content: "What planet do humans live on?", role: "user" }
],
$anthropicApiKey,
{apiType: 'ANTHROPIC', max_tokens: 2}
)
----

.Default Anthropic body parameters
.Example result
[%autowidth, opts=header]
|===
|name | description
| 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"
}
|===




Also, we can use the `apoc.ml.openai.completion` procedure to leverage the https://docs.anthropic.com/en/api/complete[Anthropic Complete API].

These are the default key-value parameters that will be included in the body request, if not specified:

.Default Anthropic key-value parameters
[%autowidth, opts=header]
|===
| key | value
| max_tokens_to_sample | 1000
| model | claude-2.1
| model | "claude-2.1"
|===


For example:

[source,cypher]
----
CALL apoc.ml.openai.completion('\n\nHuman: What color is sky?\n\nAssistant:',
$anthropicApiKey,
{apiType: 'ANTHROPIC'}
)
----

.Example result
[%autowidth, opts=header]
|===
| 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"
}
|===

Moreover, we can specify other custom body requests, like the `max_tokens_to_sample` value, to be included in the config parameter:

[source,cypher]
----
CALL apoc.ml.openai.completion('\n\nHuman: What color is sky?\n\nAssistant:',
$anthropicApiKey,
{apiType: 'ANTHROPIC', max_tokens_to_sample: 3}
)
----

.Example result
[%autowidth, opts=header]
|===
| value
| {
"id": "compl_015yzL9jDdMQnLSN3jkQifZt",
"stop": null,
"model": "claude-2.1",
"stop_reason": "max_tokens",
"type": "completion",
"completion": " The sky is",
"log_id": "compl_015yzL9jDdMQnLSN3jkQifZt"
}
|===


And also, we can specify the API version via `anthropic-version` configuration parameter, like the above example with the apoc.ml.openai.chat procedure.


[NOTE]
====
Expand Down
8 changes: 4 additions & 4 deletions extended/src/test/java/apoc/ml/OpenAIAnthropicIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void chatWithImageAnthropic() throws IOException {
byte[] fileContent = FileUtils.readFileToByteArray(new File(path));
String base64Image = Base64.getEncoder().encodeToString(fileContent);

List<Map<String, ?>> parts = List.of(
List<Map<String, ?>> contentBody = List.of(
Map.of(
"type", "image",
"source", Map.of(
Expand All @@ -98,16 +98,16 @@ public void chatWithImageAnthropic() throws IOException {

List<Map<String, Object>> messages = List.of(Map.of(
"role", "user",
"content", parts
"content", contentBody
));

String query = "CALL apoc.ml.openai.chat($content, $apiKey, $conf)";
String query = "CALL apoc.ml.openai.chat($messages, $apiKey, $conf)";

Map<String, Object> conf = Map.of(
API_TYPE_CONF_KEY, ANTHROPIC.name()
);
testCall(db, query,
Map.of( "content", messages,"conf", conf, "apiKey", anthropicApiKey),
Map.of( "messages", messages,"conf", conf, "apiKey", anthropicApiKey),
(row) -> {
var result = (Map<String,Object>) row.get("value");
var contentList = (List<Map<String, Object>>) result.get("content");
Expand Down

0 comments on commit 4b2fc02

Please sign in to comment.