This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
forked from LangStream/langstream
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Openai embedding dimensions and newer Azure SDK (#62)
- Loading branch information
1 parent
eca29ef
commit 5927f8c
Showing
17 changed files
with
374 additions
and
64 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
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
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
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
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
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
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
107 changes: 107 additions & 0 deletions
107
.../langstream-ai-agents/src/test/java/ai/langstream/ai/agents/services/impl/OpenAITest.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,107 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.langstream.ai.agents.services.impl; | ||
|
||
import com.azure.ai.openai.OpenAIAsyncClient; | ||
import com.azure.ai.openai.OpenAIClient; | ||
import com.azure.ai.openai.OpenAIClientBuilder; | ||
import com.azure.ai.openai.models.ChatCompletionsOptions; | ||
import com.azure.ai.openai.models.ChatRequestMessage; | ||
import com.azure.ai.openai.models.ChatRequestUserMessage; | ||
import com.azure.ai.openai.models.EmbeddingItem; | ||
import com.azure.ai.openai.models.Embeddings; | ||
import com.azure.ai.openai.models.EmbeddingsOptions; | ||
import com.azure.ai.openai.models.EmbeddingsUsage; | ||
import com.azure.core.credential.AzureKeyCredential; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class OpenAITest { | ||
|
||
private OpenAIAsyncClient openAIClient; | ||
|
||
@BeforeEach | ||
void setup() { | ||
openAIClient = | ||
new OpenAIClientBuilder() | ||
.credential(new AzureKeyCredential("YOUR_OPENAI_KEY")) | ||
.buildAsyncClient(); | ||
} | ||
|
||
@Disabled | ||
@Test | ||
void testRealChatCompletions() throws Exception { | ||
List<ChatRequestMessage> chatMessages = new ArrayList<>(); | ||
chatMessages.add(new ChatRequestUserMessage("Name the US presidents of the 20th century")); | ||
|
||
ChatCompletionsOptions options = new ChatCompletionsOptions(chatMessages); | ||
|
||
CompletableFuture<Void> completableFuture = new CompletableFuture<>(); | ||
|
||
openAIClient | ||
.getChatCompletionsStream("gpt-3.5-turbo", options) | ||
.doOnNext( | ||
chatCompletions -> { | ||
String response = | ||
chatCompletions.getChoices().get(0).getDelta().getContent(); | ||
System.out.println("Response: " + response); | ||
if (response == null) { | ||
completableFuture.complete(null); | ||
} | ||
}) | ||
.doOnError(completableFuture::completeExceptionally) | ||
.subscribe(); | ||
|
||
completableFuture.join(); | ||
} | ||
|
||
@Disabled | ||
@Test | ||
void testRealEmbeddings() throws Exception { | ||
String azureOpenaiKey = ""; | ||
String deploymentOrModelId = "text-embedding-ada-002"; | ||
|
||
OpenAIClient client = | ||
new OpenAIClientBuilder() | ||
.credential(new AzureKeyCredential(azureOpenaiKey)) | ||
.buildClient(); | ||
|
||
EmbeddingsOptions embeddingsOptions = | ||
new EmbeddingsOptions(Arrays.asList("Your text string goes here")); | ||
|
||
Embeddings embeddings = client.getEmbeddings(deploymentOrModelId, embeddingsOptions); | ||
|
||
for (EmbeddingItem item : embeddings.getData()) { | ||
System.out.printf("Index: %d.%n", item.getPromptIndex()); | ||
System.out.println( | ||
"Embedding as base64 encoded string: " + item.getEmbeddingAsString()); | ||
System.out.println("Embedding as list of floats: "); | ||
for (Float embedding : item.getEmbedding()) { | ||
System.out.printf("%f;", embedding); | ||
} | ||
} | ||
|
||
EmbeddingsUsage usage = embeddings.getUsage(); | ||
System.out.printf( | ||
"Usage: number of prompt token is %d and number of total tokens in request and response is %d.%n", | ||
usage.getPromptTokens(), usage.getTotalTokens()); | ||
} | ||
} |
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
Oops, something went wrong.