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: Implement .batch support #370

Merged
merged 1 commit into from
Apr 9, 2024
Merged

feat: Implement .batch support #370

merged 1 commit into from
Apr 9, 2024

Conversation

davidmigloz
Copy link
Owner

@davidmigloz davidmigloz commented Apr 9, 2024

Related to #266

Batches the invocation of the Runnable on the given inputs.

final model = ChatOpenAI(apiKey: openaiApiKey);
final promptTemplate = ChatPromptTemplate.fromTemplate(
  'Tell me a joke about {topic}',
);
final chain = promptTemplate | model | StringOutputParser();

final res = await chain.batch([
  {'topic': 'bears'},
  {'topic': 'cats'},
]);
print(res);
//['Why did the bear break up with his girlfriend? Because she was too "grizzly" for him!',
// 'Why was the cat sitting on the computer? Because it wanted to keep an eye on the mouse!']

If the underlying provider supports batching, this method will try to batch the calls to the provider. Otherwise, it will just call invoke on each input concurrently. You can configure the concurrency limit by setting the concurrencyLimit field in the options parameter.

You can provide call options to the batch method using the options parameter. It can be:

  • null: the default options are used.
  • List with 1 element: the same options are used for all inputs.
  • List with the same length as the inputs: each input gets its own options.
final res = await chain.batch(
  [
    {'topic': 'bears'},
    {'topic': 'cats'},
  ],
  options: [
    const ChatOpenAIOptions(model: 'gpt-3.5-turbo', temperature: 0.5),
    const ChatOpenAIOptions(model: 'gpt-4', temperature: 0.7),
  ],
);
print(res);
//['Why did the bear break up with his girlfriend? Because he couldn't bear the relationship anymore!,',
// 'Why don't cats play poker in the jungle? Because there's too many cheetahs!']

@davidmigloz davidmigloz self-assigned this Apr 9, 2024
@davidmigloz davidmigloz added t:enhancement New feature or request c:lcel LangChain Expression Language labels Apr 9, 2024
@davidmigloz davidmigloz added this to the v0.5.0 milestone Apr 9, 2024
@davidmigloz davidmigloz merged commit d254f92 into main Apr 9, 2024
1 check passed
@davidmigloz davidmigloz deleted the batch branch April 9, 2024 21:07
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:lcel LangChain Expression Language t:enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant