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: Include usage stats when streaming with OpenAI and ChatOpenAI #406

Merged
merged 1 commit into from
May 9, 2024

Conversation

davidmigloz
Copy link
Owner

@davidmigloz davidmigloz commented May 9, 2024

final promptTemplate = ChatPromptTemplate.fromTemplates(const [
  (ChatMessageType.system, 'You are a helpful assistant that replies only with numbers in order without any spaces or commas'),
  (ChatMessageType.human, 'List the numbers from 1 to {max_num}'),
]);
final chat = ChatOpenAI(apiKey: openaiApiKey);

final chain = promptTemplate.pipe(chat);

ChatResult? result;
final stream = chain.stream({'max_num': '9'});
await for (final ChatResult res in stream) {
  result = result?.concat(res) ?? res;
}
expect(result!.output.content, contains('123456789'));
expect(result.usage.promptTokens, greaterThan(0));
expect(result.usage.responseTokens, greaterThan(0));
expect(result.usage.totalTokens, greaterThan(0));

@davidmigloz davidmigloz self-assigned this May 9, 2024
@davidmigloz davidmigloz added t:enhancement New feature or request p:langchain_openai langchain_openai package. labels May 9, 2024
@davidmigloz davidmigloz added this to the v0.8.0 milestone May 9, 2024
@davidmigloz davidmigloz merged commit 9d34668 into main May 9, 2024
1 check passed
@davidmigloz davidmigloz deleted the usage-streaming branch May 9, 2024 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p:langchain_openai langchain_openai package. t:enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

1 participant