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 additive options merging for cascade bind calls #500

Merged
merged 1 commit into from
Jul 20, 2024

Conversation

davidmigloz
Copy link
Owner

@davidmigloz davidmigloz commented Jul 20, 2024

Relates to #481

Previously, if you had multiple bind calls in cascade, the options from the previous bind call were overwritten by the following bind call. This PR introduces a change where options provided in successive bind calls are now merged additively.

Non-null fields in the options of a subsequent bind call will override those in the existing options, while preserving any other existing options that were not explicitly overridden.

Example:

final chatModel = ChatOpenAI(
  apiKey: openAiApiKey,
  defaultOptions: const ChatOpenAIOptions(
    model: 'gpt-4o',
    temperature: 0,
  ),
);

final chatModelWithTools = chatModel.bind(
  const ChatOpenAIOptions(
    tools: [getCurrentWeatherTool, jokeTool],
  ),
);

final res1 = await chatModelWithTools.invoke(
  PromptValue.string(
    'Tell me the weather in Barcelona, Spain and a joke about bears',
  ),
);
print(res1.output.toolCalls);
// [AIChatMessageToolCall{
//   id: call_b7WEVe4FsnNMPdxoGnwnYmE1,
//   name: get_current_weather,
//   argumentsRaw: {"location": "Barcelona, Spain"},
//   arguments: {location: Barcelona, Spain},
// }, AIChatMessageToolCall{
//   id: call_TIhoSK8pfAi7KaOd0rCDvj3C,
//   name: joke,
//   argumentsRaw: {"setup": "Why don't bears wear shoes?", "punchline": "Because they have bear feet!"},
//   arguments: {setup: Why don't bears wear shoes?, punchline: Because they have bear feet!},
// }]

final chatModelForceWeatherTool = chatModelWithTools.bind(
  ChatOpenAIOptions(
    toolChoice: ChatToolChoice.forced(name: getCurrentWeatherTool.name),
  ),
);

final res2 = await chatModelForceWeatherTool.invoke(
  PromptValue.string(
    'Tell me the weather in Barcelona, Spain and a joke about bears',
  ),
);
print(res2.output.toolCalls);
// [AIChatMessageToolCall{
//   id: call_halcHZRWDLgs82MJW3WkUitg,
//   name: get_current_weather,
//   argumentsRaw: {"location":"Barcelona, Spain","unit":"celsius"},
//   arguments: {location: Barcelona, Spain, unit: celsius},
// }]

final chatModelForceJokeTool = chatModelWithTools.bind(
  ChatOpenAIOptions(
    toolChoice: ChatToolChoice.forced(name: jokeTool.name),
  ),
);

final res3 = await chatModelForceJokeTool.invoke(
  PromptValue.string(
    'Tell me the weather in Barcelona, Spain and a joke about bears',
  ),
);
print(res3.output.toolCalls);
// [AIChatMessageToolCall{
//   id: call_CnijbH1AQNnSh2XEG2JbKcsZ,
//   name: joke,
//   argumentsRaw: {"punchline":"Because they always bring their own bear-ings!"},
//   arguments: {punchline: Because they always bring their own bear-ings!},
// }]

@davidmigloz davidmigloz self-assigned this Jul 20, 2024
@davidmigloz davidmigloz added t:enhancement New feature or request c:lcel LangChain Expression Language labels Jul 20, 2024
@davidmigloz davidmigloz added this to the v0.8.0 milestone Jul 20, 2024
@davidmigloz davidmigloz merged commit 8691eb2 into main Jul 20, 2024
1 check passed
@davidmigloz davidmigloz deleted the merge-bind branch July 20, 2024 16:21
KennethKnudsen97 pushed a commit to KennethKnudsen97/langchain_dart that referenced this pull request Oct 1, 2024
KennethKnudsen97 pushed a commit to KennethKnudsen97/langchain_dart that referenced this pull request Oct 1, 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