Skip to content
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

feat: Support Predicted Outputs in openai_dart #613

Merged
merged 1 commit into from
Dec 6, 2024

Conversation

davidmigloz
Copy link
Owner

@davidmigloz davidmigloz commented Dec 6, 2024

Predicted Outputs enable you to speed up API responses from Chat Completions when many of the output tokens are known ahead of time. This is most common when you are regenerating a text or code file with minor modifications. You can provide your prediction using the prediction request parameter in Chat Completions.

Predicted Outputs are available today using the latest gpt-4o and gpt-4o-mini models. Read on to learn how to use Predicted Outputs to reduce latency in your applicatons.

More info: https://platform.openai.com/docs/guides/predicted-outputs

Example:

const codeContent = '''
class User {
  firstName: string = "";
  lastName: string = "";
  username: string = "";
}

export default User;
''';

const request = CreateChatCompletionRequest(
  model: ChatCompletionModel.model(
    ChatCompletionModels.gpt4o,
  ),
  messages: [
    ChatCompletionMessage.user(
      content: ChatCompletionUserMessageContent.string(
        'Replace the username property with an email property. '
        'Respond only with code, and with no markdown formatting.',
      ),
    ),
    ChatCompletionMessage.user(
      content: ChatCompletionUserMessageContent.string(codeContent),
    ),
  ],
  prediction: PredictionContent(
    content: PredictionContentContent.text(codeContent),
  ),
);
final res1 = await client.createChatCompletion(request: request);
final choice1 = res1.choices.first;
print(choice1.message.content);
// class User {
//   firstName: string = "";
//   lastName: string = "";
//   email: string = "";
// }
// 
// export default User;

print(res1.usage?.completionTokensDetails?.acceptedPredictionTokens)
// 18
print(res1.usage?.completionTokensDetails?.rejectedPredictionTokens)
// 10

@davidmigloz davidmigloz self-assigned this Dec 6, 2024
@davidmigloz davidmigloz added t:enhancement New feature or request p:openai_dart openai_dart package. labels Dec 6, 2024
@davidmigloz davidmigloz added this to the 0.8.0 milestone Dec 6, 2024
@davidmigloz davidmigloz merged commit 315fe0f into main Dec 6, 2024
3 checks passed
@davidmigloz davidmigloz deleted the predicted-outputs branch December 6, 2024 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p:openai_dart openai_dart package. t:enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant