|
| 1 | +--- |
| 2 | +title: Chat Completions |
| 3 | +--- |
| 4 | + |
| 5 | +!!! note |
| 6 | + |
| 7 | + Support the Open Ai service provided by Microsoft, product address: [https://azure.microsoft.com/zh-cn/products/cognitive-services/openai-service/](https://azure.microsoft.com/zh-cn/products/cognitive-services/openai-service/) |
| 8 | + |
| 9 | +### Required |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +!!! note |
| 14 | + |
| 15 | + The following are some configurations that must be specified to invoke the service. |
| 16 | + |
| 17 | +| name | description | |
| 18 | +|:----------:|-----------------------------------------------------------------------------| |
| 19 | +| `apiHost` | Created zone markers in the format `${your-resource-name}.openai.azure.com` | |
| 20 | +| `apiKey` | Azure token | |
| 21 | +| `provider` | Specify `ProviderModel.azure` | |
| 22 | +| `model` | Model name deployed in Azure | |
| 23 | +| `version` | Model version deployed in Azure | |
| 24 | + |
| 25 | +### Create chat completion |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +Creates a model response for the given chat conversation. |
| 30 | + |
| 31 | +```java |
| 32 | +// Automatic resource release |
| 33 | +try(OpenAiClient client=OpenAiClient.builder() |
| 34 | + .apiHost("https://eus-chatgpt.openai.azure.com") |
| 35 | + .apiKey(System.getProperty("azure.token")) |
| 36 | + .provider(ProviderModel.azure) |
| 37 | + .model("text-davinci-002") |
| 38 | + .version("2022-12-01") |
| 39 | + .build()) |
| 40 | +{ |
| 41 | + List<CompletionMessageEntity> messages = Lists.newArrayList(); |
| 42 | + messages.add(CompletionMessageEntity.builder() |
| 43 | + .content("Hello, my name is openai-java-sdk") |
| 44 | + .build()); |
| 45 | + |
| 46 | + CompletionChatEntity configure = CompletionChatEntity.builder() |
| 47 | + .messages(messages) |
| 48 | + .build(); |
| 49 | + |
| 50 | + client.createChatCompletion(configure) |
| 51 | + .getChoices() |
| 52 | + .forEach(choice -> messages.add(choice.getMessage())); |
| 53 | + |
| 54 | + messages.add(CompletionMessageEntity.builder() |
| 55 | + .content("What is my name?") |
| 56 | + .build()); |
| 57 | + client.createChatCompletion(configure).getChoices(); |
| 58 | +} |
| 59 | +``` |
| 60 | + |
0 commit comments