From 4b2fc029c45b6f40db7ecfcc855eb2e6ea74d0b5 Mon Sep 17 00:00:00 2001 From: vga91 Date: Tue, 30 Jul 2024 09:44:45 +0200 Subject: [PATCH] added doc examples --- .../modules/ROOT/pages/ml/openai.adoc | 183 +++++++++++++++++- .../test/java/apoc/ml/OpenAIAnthropicIT.java | 8 +- 2 files changed, 177 insertions(+), 14 deletions(-) diff --git a/docs/asciidoc/modules/ROOT/pages/ml/openai.adoc b/docs/asciidoc/modules/ROOT/pages/ml/openai.adoc index 65ce56a905..356e6984b7 100644 --- a/docs/asciidoc/modules/ROOT/pages/ml/openai.adoc +++ b/docs/asciidoc/modules/ROOT/pages/ml/openai.adoc @@ -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] ---- @@ -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: ""} } + ] + } + ], $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] ==== diff --git a/extended/src/test/java/apoc/ml/OpenAIAnthropicIT.java b/extended/src/test/java/apoc/ml/OpenAIAnthropicIT.java index dfc3ee665e..e159f77f8e 100644 --- a/extended/src/test/java/apoc/ml/OpenAIAnthropicIT.java +++ b/extended/src/test/java/apoc/ml/OpenAIAnthropicIT.java @@ -81,7 +81,7 @@ public void chatWithImageAnthropic() throws IOException { byte[] fileContent = FileUtils.readFileToByteArray(new File(path)); String base64Image = Base64.getEncoder().encodeToString(fileContent); - List> parts = List.of( + List> contentBody = List.of( Map.of( "type", "image", "source", Map.of( @@ -98,16 +98,16 @@ public void chatWithImageAnthropic() throws IOException { List> 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 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) row.get("value"); var contentList = (List>) result.get("content");