这是indexloc提供的服务,不要输入任何密码
Skip to content

Add support for capturing usage prompt_tokens_details and completion_tokens_details #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ OpenAi4J是一个非官方的Java库,旨在帮助java开发者与OpenAI的GPT
## 导入依赖
### Gradle

`implementation 'io.github.lambdua:<api|client|service>:0.22.91'`
`implementation 'io.github.lambdua:<api|client|service>:0.22.92'`
### Maven
```xml

<dependency>
<groupId>io.github.lambdua</groupId>
<artifactId>service</artifactId>
<version>0.22.91</version>
<version>0.22.92</version>
</dependency>
```

Expand Down Expand Up @@ -61,7 +61,7 @@ static void simpleChat() {
<dependency>
<groupId>io.github.lambdua</groupId>
<artifactId>api</artifactId>
<version>0.22.91</version>
<version>0.22.92</version>
</dependency>
```

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ applications effortlessly.
## Import
### Gradle

`implementation 'io.github.lambdua:<api|client|service>:0.22.91'`
`implementation 'io.github.lambdua:<api|client|service>:0.22.92'`
### Maven
```xml

<dependency>
<groupId>io.github.lambdua</groupId>
<artifactId>service</artifactId>
<version>0.22.91</version>
<version>0.22.92</version>
</dependency>
```

Expand Down Expand Up @@ -67,7 +67,7 @@ To utilize pojos, import the api module:
<dependency>
<groupId>io.github.lambdua</groupId>
<artifactId>api</artifactId>
<version>0.22.91</version>
<version>0.22.92</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.lambdua</groupId>
<artifactId>openai-java</artifactId>
<version>0.22.91</version>
<version>0.22.92</version>
</parent>
<packaging>jar</packaging>
<artifactId>api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.theokanning.openai;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

/**
* Breakdown of tokens used in a completion.
*/
@Data
public class CompletionTokensDetails {

@JsonProperty("reasoning_tokens")
long reasoningTokens;

@JsonProperty("audio_tokens")
long audioTokens;

@JsonProperty("accepted_prediction_tokens")
long acceptedPredictionTokens;

@JsonProperty("rejected_prediction_tokens")
long rejectedPredictionTokens;
}
22 changes: 22 additions & 0 deletions api/src/main/java/com/theokanning/openai/PromptTokensDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.theokanning.openai;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

/**
* Breakdown of tokens used in the prompt.
*/
@Data
public class PromptTokensDetails {
/**
* Cached tokens present in the prompt.
*/
@JsonProperty("cached_tokens")
long cachedTokens;

/**
* Audio input tokens present in the prompt.
*/
@JsonProperty("audio_tokens")
long audioTokens;
}
12 changes: 12 additions & 0 deletions api/src/main/java/com/theokanning/openai/Usage.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ public class Usage {
*/
@JsonProperty("total_tokens")
long totalTokens;

/**
* Breakdown of tokens used in the prompt.
*/
@JsonProperty("prompt_tokens_details")
PromptTokensDetails promptTokensDetails;

/**
* Breakdown of tokens used in a completion.
*/
@JsonProperty("completion_tokens_details")
CompletionTokensDetails completionTokensDetails;
}
10 changes: 9 additions & 1 deletion api/src/test/resources/fixtures/ChatCompletionResult.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
"total_tokens": 21,
"prompt_tokens_details": {
"cached_tokens": 0
},
"completion_tokens_details": {
"reasoning_tokens": 0,
"accepted_prediction_tokens": 0,
"rejected_prediction_tokens": 0
}
}
}
2 changes: 1 addition & 1 deletion client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.lambdua</groupId>
<artifactId>openai-java</artifactId>
<version>0.22.91</version>
<version>0.22.92</version>
</parent>
<packaging>jar</packaging>

Expand Down
4 changes: 2 additions & 2 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.lambdua</groupId>
<artifactId>example</artifactId>
<version>0.22.91</version>
<version>0.22.92</version>
<name>example</name>

<properties>
Expand All @@ -17,7 +17,7 @@
<dependency>
<groupId>io.github.lambdua</groupId>
<artifactId>service</artifactId>
<version>0.22.91</version>
<version>0.22.92</version>
</dependency>

</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.lambdua</groupId>
<artifactId>openai-java</artifactId>
<version>0.22.91</version>
<version>0.22.92</version>
<packaging>pom</packaging>
<description>openai java 版本</description>
<url>https://github.com/Lambdua/openai-java</url>
Expand Down
2 changes: 1 addition & 1 deletion service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.lambdua</groupId>
<artifactId>openai-java</artifactId>
<version>0.22.91</version>
<version>0.22.92</version>
</parent>
<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ void createChatCompletion() {
.logitBias(new HashMap<>())
.build();

List<ChatCompletionChoice> choices = service.createChatCompletion(chatCompletionRequest).getChoices();
assertEquals(5, choices.size());
ChatCompletionResult chatCompletionResult = service.createChatCompletion(chatCompletionRequest);
assertEquals(5, chatCompletionResult.getChoices().size());
assertNotNull(chatCompletionResult.getUsage());
assertNotNull(chatCompletionResult.getUsage().getPromptTokensDetails());
assertNotNull(chatCompletionResult.getUsage().getCompletionTokensDetails());
}

@Test
Expand Down