Skip to content

Commit

Permalink
Added convenience getContent
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesDuboisSAP committed Sep 4, 2024
1 parent 468ba8b commit f9f10f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ See [an example pom in our Spring Boot application](e2e-test-app/pom.xml)
final OpenAiChatCompletionOutput result =
OpenAiClient.forModel(GPT_35_TURBO).chatCompletion("Hello World! Why is this phrase so famous?");

final String resultMessage = result.getChoices().get(0).getMessage().getContent();
final String resultMessage = result.getContent();
```

### Chat completion message history
### Message history

```java
final var systemMessage =
Expand All @@ -201,7 +201,7 @@ final var request =
final OpenAiChatCompletionOutput result =
OpenAiClient.forModel(GPT_35_TURBO).chatCompletion(request);

final String resultMessage = result.getChoices().get(0).getMessage().getContent();
final String resultMessage = result.getContent();
```

See [an example in our Spring Boot application](e2e-test-app/src/main/java/com/sap/ai/sdk/app/controllers/OpenAiController.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nonnull;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand All @@ -25,4 +26,16 @@ public class OpenAiChatCompletionOutput extends OpenAiCompletionOutput {
@JsonProperty("system_fingerprint")
@Getter(onMethod_ = @Nonnull)
private String systemFingerprint;

/**
* Get the message content from the output.
*
* <p>Note: If there are multiple choices only the first one is returned
*
* @return the message content or empty string.
*/
@Nonnull
public String getContent() {
return Objects.requireNonNullElse(getChoices().get(0).getMessage().getContent(), "");
}
}

0 comments on commit f9f10f4

Please sign in to comment.