langchain/libs
ccurme 9a010fb761
openai: read stream_options (#21548)
OpenAI recently added a `stream_options` parameter to its chat
completions API (see [release
notes](https://platform.openai.com/docs/changelog/added-chat-completions-stream-usage)).
When this parameter is set to `{"usage": True}`, an extra "empty"
message is added to the end of a stream containing token usage. Here we
propagate token usage to `AIMessage.usage_metadata`.

We enable this feature by default. Streams would now include an extra
chunk at the end, **after** the chunk with
`response_metadata={'finish_reason': 'stop'}`.

New behavior:
```
[AIMessageChunk(content='', id='run-4b20dbe0-3817-4f62-b89d-03ef76f25bde'),
 AIMessageChunk(content='Hello', id='run-4b20dbe0-3817-4f62-b89d-03ef76f25bde'),
 AIMessageChunk(content='!', id='run-4b20dbe0-3817-4f62-b89d-03ef76f25bde'),
 AIMessageChunk(content='', response_metadata={'finish_reason': 'stop'}, id='run-4b20dbe0-3817-4f62-b89d-03ef76f25bde'),
 AIMessageChunk(content='', id='run-4b20dbe0-3817-4f62-b89d-03ef76f25bde', usage_metadata={'input_tokens': 8, 'output_tokens': 9, 'total_tokens': 17})]
```

Old behavior (accessible by passing `stream_options={"include_usage":
False}` into (a)stream:
```
[AIMessageChunk(content='', id='run-1312b971-c5ea-4d92-9015-e6604535f339'),
 AIMessageChunk(content='Hello', id='run-1312b971-c5ea-4d92-9015-e6604535f339'),
 AIMessageChunk(content='!', id='run-1312b971-c5ea-4d92-9015-e6604535f339'),
 AIMessageChunk(content='', response_metadata={'finish_reason': 'stop'}, id='run-1312b971-c5ea-4d92-9015-e6604535f339')]
```

From what I can tell this is not yet implemented in Azure, so we enable
only for ChatOpenAI.
2024-05-24 13:20:56 -04:00
..
cli cli: model name substitution fix, release 0.0.23 (#22089) 2024-05-23 13:09:38 -07:00
community community[patch]: Put authorized identities behind a feature flag in SharepointLoader (#22125) 2024-05-24 12:42:57 -04:00
core docs: fix some spelling mistakes caught by newest version of code spell (#22090) 2024-05-23 16:59:11 -04:00
experimental infra: rm unused # noqa violations (#22049) 2024-05-22 15:21:08 -07:00
langchain docs: fix some spelling mistakes caught by newest version of code spell (#22090) 2024-05-23 16:59:11 -04:00
partners openai: read stream_options (#21548) 2024-05-24 13:20:56 -04:00
standard-tests core, partners: add token usage attribute to AIMessage (#21944) 2024-05-23 14:21:58 -04:00
text-splitters text-splitters[patch]: Extend TextSplitter:keep_separator functionality (#21130) 2024-05-22 13:17:45 -07:00