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(chat-models): Support seed, system_fingerprint and JSON Mode in ChatOpenAI #205

Merged
merged 1 commit into from
Nov 7, 2023

Conversation

davidmigloz
Copy link
Owner

@davidmigloz davidmigloz commented Nov 7, 2023

Seed example:

final prompt = PromptValue.string('How are you?');
final llm = ChatOpenAI(
  apiKey: openaiApiKey,
  temperature: 0,
  seed: 9999,
);
final res1 = await llm.invoke(prompt);
expect(res1.generations, hasLength(1));
final generation1 = res1.generations.first;
final res2 = await llm.invoke(prompt);
expect(res2.generations, hasLength(1));
final generation2 = res2.generations.first;
expect(
  res1.modelOutput?['system_fingerprint'],
  res2.modelOutput?['system_fingerprint'],
);
expect(generation1.output, generation2.output);

JSON mode example:

 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,
   seed: 9999,
   responseFormat: const ChatOpenAIResponseFormat(
     type: ChatOpenAIResponseFormatType.jsonObject,
   ),
 );
 final res = await llm.invoke(prompt);
 expect(res.generations, hasLength(1));
 final outputMsg = res.generations.first.output;
 final outputJson = json.decode(outputMsg.content) as Map<String, dynamic>;
 expect(outputJson['companies'], isNotNull);
 final companies = outputJson['companies'] as List<dynamic>;
 expect(companies, hasLength(2));
 final firstCompany = companies.first as Map<String, dynamic>;
 expect(firstCompany['name'], 'Google');
 expect(firstCompany['origin'], 'USA');
 final secondCompany = companies.last as Map<String, dynamic>;
 expect(secondCompany['name'], 'Deepmind');
 expect(secondCompany['origin'], 'UK');

@davidmigloz davidmigloz self-assigned this Nov 7, 2023
@davidmigloz davidmigloz added t:enhancement New feature or request c:chat-models Chat models. p:langchain_openai langchain_openai package. labels Nov 7, 2023
@davidmigloz davidmigloz added this to the v0.0.15 milestone Nov 7, 2023
@davidmigloz davidmigloz merged commit 3332c22 into main Nov 7, 2023
1 check passed
@davidmigloz davidmigloz deleted the json branch November 7, 2023 20:38
KennethKnudsen97 pushed a commit to KennethKnudsen97/langchain_dart that referenced this pull request Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c:chat-models Chat models. 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.

1 participant