-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…5586) * core[minor],openai[patch]: Add usage metadata to base ai message * chore: lint files * Only set stream_options if the model is OpenAI * add test confirming last finish reason is stop * chore: lint files * docs * lint n format * cr * cr * Remove Azure specific check * Remove comment --------- Co-authored-by: jacoblee93 <[email protected]>
- Loading branch information
1 parent
fc67984
commit 7d8fa2e
Showing
5 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
examples/src/models/chat/integration_openai_stream_tokens.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { AIMessageChunk } from "@langchain/core/messages"; | ||
import { ChatOpenAI } from "@langchain/openai"; | ||
|
||
// Instantiate the model | ||
const model = new ChatOpenAI(); | ||
|
||
const response = await model.stream("Hello, how are you?", { | ||
// Pass the stream options | ||
stream_options: { | ||
include_usage: true, | ||
}, | ||
}); | ||
|
||
// Iterate over the response, only saving the last chunk | ||
let finalResult: AIMessageChunk | undefined; | ||
for await (const chunk of response) { | ||
if (finalResult) { | ||
finalResult = finalResult.concat(chunk); | ||
} else { | ||
finalResult = chunk; | ||
} | ||
} | ||
|
||
console.log(finalResult?.usage_metadata); | ||
|
||
/* | ||
{ input_tokens: 13, output_tokens: 30, total_tokens: 43 } | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters