|
| 1 | +--- |
| 2 | +title: Completions |
| 3 | +--- |
| 4 | + |
| 5 | +!!! Note |
| 6 | + |
| 7 | + 调用前请先构建客户端,构建代码如下: |
| 8 | + |
| 9 | + ```java |
| 10 | + OpenAiClient client = OpenAiClient.builder() |
| 11 | + .apiHost("https://api.openai.com") |
| 12 | + .apiKey(System.getProperty("openai.token")) |
| 13 | + .build(); |
| 14 | + ``` |
| 15 | + |
| 16 | + `System.getProperty("openai.token")` 是访问 API 授权的关键。 |
| 17 | + |
| 18 | +### Create completion |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +为所提供的提示和参数创建一个完整的结果。 |
| 23 | + |
| 24 | +```java |
| 25 | +CompletionEntity configure = CompletionEntity.builder() |
| 26 | + .model(CompleteModel.TEXT_DAVINCI_003.getName()) |
| 27 | + .prompt("How to create a completion") |
| 28 | + .temperature(2D) |
| 29 | + .build(); |
| 30 | +client.createCompletion(configure); |
| 31 | +``` |
| 32 | + |
| 33 | +Body: |
| 34 | + |
| 35 | +| Name | Type | Required | |
| 36 | +|:------------------:|:--------------:|----------| |
| 37 | +| `model` | `String` | Yes | |
| 38 | +| `prompt` | `String` | Yes | |
| 39 | +| `temperature` | `Number` | No | |
| 40 | +| `maxTokens` | `Number` | No | |
| 41 | +| `topP` | `Number` | No | |
| 42 | +| `bestOf` | `Number` | No | |
| 43 | +| `frequencyPenalty` | `Number` | No | |
| 44 | +| `presencePenalty` | `Number` | No | |
| 45 | +| `stop` | `List<String>` | No | |
| 46 | + |
| 47 | +Returns: |
| 48 | + |
| 49 | +```json |
| 50 | +{ |
| 51 | + "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7", |
| 52 | + "object": "text_completion", |
| 53 | + "created": 1589478378, |
| 54 | + "model": "text-davinci-003", |
| 55 | + "choices": [ |
| 56 | + { |
| 57 | + "text": "\n\nThis is indeed a test", |
| 58 | + "index": 0, |
| 59 | + "logprobs": null, |
| 60 | + "finish_reason": "length" |
| 61 | + } |
| 62 | + ], |
| 63 | + "usage": { |
| 64 | + "prompt_tokens": 5, |
| 65 | + "completion_tokens": 7, |
| 66 | + "total_tokens": 12 |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
0 commit comments