Skip to content

Commit

Permalink
docs: Update GPT-4 Turbo JSON mode docs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Nov 7, 2023
1 parent 3332c22 commit 752edd4
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 14 deletions.
7 changes: 0 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Change Log

All notable changes to this project will be documented in this file following
[Conventional Commits](https://conventionalcommits.org) specification.

Check out the #announcements channel in the [LangChain.dart Discord](https://discord.gg/x4qbhqecVR)
server for more details about each release.

Expand Down Expand Up @@ -38,10 +35,6 @@ Packages with other changes:
- **FEAT**(vector-stores): Upgrade pinecone client to v0.6.0 ([#188](https://github.com/davidmigloz/langchain_dart/issues/188)). ([57e2587f](https://github.com/davidmigloz/langchain_dart/commit/57e2587fa3849e7aea199dd52e2cb2ce4f61946a))
- **DOCS**: Update CHANGELOG.md. ([5ea4e532](https://github.com/davidmigloz/langchain_dart/commit/5ea4e5326e706a52d157284a281eb881e05117c5))

# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## 2023-11-02

Expand Down
15 changes: 11 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,25 @@ software, even patch releases may contain
To create a release, run:

```bash
melos version -V langchain:x.x.x --no-private
melos version --no-private --no-git-tag-version
```

Or if you want to define the version manually:

```bash
melos version -V langchain:x.x.x --no-private --no-git-tag-version
```

This command will bump the version in the `langchain` package and all packages that depend on it.
It will also update the CHANGELOG.md for each package, commit the changes and creates tags.
It will also update the CHANGELOG.md for each package and commit the changes.

Create a new release on GitHub and copy the CHANGELOG.md content into the release description.
Update any CHANGELOG.md details if needed. Then create a new release on GitHub and copy the
CHANGELOG.md content into the release description.

After that, you can publish the package to pub.dev:

```bash
melos publish --no-dry-run
melos publish --no-dry-run --git-tag-version
```

Finally, drop by the [Discord](https://discord.gg/x4qbhqecVR) and let everyone know about the new
Expand Down
40 changes: 40 additions & 0 deletions docs/modules/model_io/models/chat_models/integrations/openai.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,43 @@ await stream.forEach(print);
// {setup: Why don't bears like fast food?, punchline: Because they can't catch it}
// {setup: Why don't bears like fast food?, punchline: Because they can't catch it!}
```

## JSON mode

GPT-4 Turbo supports a new JSON mode, which ensures the model will respond with valid JSON. JSON mode is useful for developers generating JSON in the Chat Completions API outside of function calling.

```dart
final openaiApiKey = Platform.environment['OPENAI_API_KEY'];
final prompt = PromptValue.chat([
ChatMessage.system(
"Extract the 'name' and 'origin' of any companies mentioned in the "
'following statement. Return a JSON list.',
),
ChatMessage.human(
'Google was founded in the USA, while Deepmind was founded in the UK',
),
]);
final llm = ChatOpenAI(
apiKey: openaiApiKey,
model: 'gpt-4-1106-preview',
temperature: 0,
responseFormat: const ChatOpenAIResponseFormat(
type: ChatOpenAIResponseFormatType.jsonObject,
),
);
final res = await llm.invoke(prompt);
final outputMsg = res.firstOutputAsString;
print(outputMsg);
// {
// "companies": [
// {
// "name": "Google",
// "origin": "USA"
// },
// {
// "name": "Deepmind",
// "origin": "UK"
// }
// ]
// }
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ void main(final List<String> arguments) async {
await _chatOpenAI();
await _chatOpenAIStreaming();
await _chatOpenAIStreamingFunctions();
await _chatOpenAIJsonMode();
}

Future<void> _chatOpenAI() async {
Expand Down Expand Up @@ -115,3 +116,40 @@ Future<void> _chatOpenAIStreamingFunctions() async {
// {setup: Why don't bears like fast food?, punchline: Because they can't catch it}
// {setup: Why don't bears like fast food?, punchline: Because they can't catch it!}
}

Future<void> _chatOpenAIJsonMode() async {
final openaiApiKey = Platform.environment['OPENAI_API_KEY'];

final prompt = PromptValue.chat([
ChatMessage.system(
"Extract the 'name' and 'origin' of any companies mentioned in the "
'following statement. Return a JSON list.',
),
ChatMessage.human(
'Google was founded in the USA, while Deepmind was founded in the UK',
),
]);
final llm = ChatOpenAI(
apiKey: openaiApiKey,
model: 'gpt-4-1106-preview',
temperature: 0,
responseFormat: const ChatOpenAIResponseFormat(
type: ChatOpenAIResponseFormatType.jsonObject,
),
);
final res = await llm.invoke(prompt);
final outputMsg = res.firstOutputAsString;
print(outputMsg);
// {
// "companies": [
// {
// "name": "Google",
// "origin": "USA"
// },
// {
// "name": "Deepmind",
// "origin": "UK"
// }
// ]
// }
}
3 changes: 0 additions & 3 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ command:
changelogs:
- path: CHANGELOG.md
description: |
All notable changes to this project will be documented in this file following
[Conventional Commits](https://conventionalcommits.org) specification.
Check out the #announcements channel in the [LangChain.dart Discord](https://discord.gg/x4qbhqecVR)
server for more details about each release.
packageFilters:
Expand Down

0 comments on commit 752edd4

Please sign in to comment.