-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
configurable AI endpoint; add possibility to call Anthropic Claude o…
…r local models
- Loading branch information
Showing
4 changed files
with
200 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
console/src/test/java/com/composum/sling/nodes/ai/impl/AIServiceImplClaudeCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.composum.sling.nodes.ai.impl; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.mockito.Mockito; | ||
|
||
/** | ||
* Calls Anthropic Claude create messsage service to verify the implementation works. | ||
* Not a test since that needs network and OpenAI key. | ||
* | ||
* @see "https://docs.anthropic.com/claude/reference/messages_post" | ||
*/ | ||
public class AIServiceImplClaudeCall { | ||
|
||
private static AIServiceImpl aiService = new AIServiceImpl(); | ||
|
||
public static void main(String[] args) throws Exception { | ||
setUp(); | ||
testPrompt(); | ||
} | ||
|
||
public static void setUp() throws IOException { | ||
AIServiceImpl.Configuration config = Mockito.mock(AIServiceImpl.Configuration.class); | ||
when(config.disabled()).thenReturn(false); | ||
when(config.chatCompletionUrl()).thenReturn("https://api.anthropic.com/v1/messages"); | ||
when(config.apiKeyHeader()).thenReturn("x-api-key"); | ||
when(config.defaultModel()).thenReturn("claude-3-opus-20240229"); | ||
when(config.additionalHeader()).thenReturn("anthropic-version"); | ||
String version = StringUtils.defaultIfBlank(System.getenv("ANTHROPIC_API_VERSION"), "2023-06-01"); | ||
when (config.additionalHeaderValue()).thenReturn(version); | ||
when(config.openAiApiKey()).thenReturn(null); // rely on env var ANTHROPIC_API_KEY | ||
when(config.requestsPerMinuteLimit()).thenReturn(20); | ||
when(config.requestsPerHourLimit()).thenReturn(60); | ||
when(config.requestsPerDayLimit()).thenReturn(120); | ||
when(config.maxTokens()).thenReturn(2048); | ||
aiService.activate(config); | ||
if (!aiService.isAvailable()) { | ||
throw new IllegalStateException("AI service not available, probably because of missing ANTHROPIC_API_KEY"); | ||
} | ||
} | ||
|
||
public static void testPrompt() throws Exception { | ||
System.out.println(aiService.prompt(null, "Hi!", null)); | ||
System.out.println(aiService.prompt("Reverse the users message.", "Hi!", null)); | ||
System.out.println(aiService.prompt(null, "Make a haiku in JSON format about Composum.", AIServiceImpl.ResponseFormat.JSON)); | ||
System.out.println(aiService.prompt("Always respond in JSON", "Make a haiku about Composum.", AIServiceImpl.ResponseFormat.JSON)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
console/src/test/java/com/composum/sling/nodes/ai/impl/AIServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.composum.sling.nodes.ai.impl; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
import com.composum.sling.nodes.ai.AIService; | ||
|
||
public class AIServiceImplTest { | ||
|
||
@Test | ||
public void extractOpenAIResponse() throws AIService.AIServiceException { | ||
String response = "{\n" + | ||
" \"id\": \"chatcmpl-123\",\n" + | ||
" \"model\": \"gpt-3.5-turbo-0125\",\n" + | ||
" \"choices\": [\n" + | ||
" {\n" + | ||
" \"index\": 0,\n" + | ||
" \"message\": {\n" + | ||
" \"role\": \"assistant\",\n" + | ||
" \"content\": \"Hello there, how may I assist you today?\"\n" + | ||
" },\n" + | ||
" \"finish_reason\": \"stop\"\n" + | ||
" }\n" + | ||
" ]\n" + | ||
"}\n"; | ||
String result = new AIServiceImpl().extractText(response); | ||
assertEquals("Hello there, how may I assist you today?", result); | ||
} | ||
|
||
@Test | ||
public void extractClaudeResponse() throws AIService.AIServiceException { | ||
String response = "{\n" + | ||
" \"content\": [\n" + | ||
" {\n" + | ||
" \"text\": \"Hi! My name is Claude.\",\n" + | ||
" \"type\": \"text\"\n" + | ||
" }\n" + | ||
" ],\n" + | ||
" \"id\": \"msg_013Zva2CMHLNnXjNJJKqJ2EF\",\n" + | ||
" \"model\": \"claude-3-opus-20240229\",\n" + | ||
" \"role\": \"assistant\",\n" + | ||
" \"stop_reason\": \"end_turn\",\n" + | ||
" \"stop_sequence\": null,\n" + | ||
" \"type\": \"message\",\n" + | ||
" \"usage\": {\n" + | ||
" \"input_tokens\": 10,\n" + | ||
" \"output_tokens\": 25\n" + | ||
" }\n" + | ||
"}"; | ||
String result = new AIServiceImpl().extractText(response); | ||
assertEquals("Hi! My name is Claude.", result); | ||
} | ||
|
||
} |