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: Add support for Runnable.mapInputStream #393

Merged
merged 1 commit into from
Apr 30, 2024
Merged

Conversation

davidmigloz
Copy link
Owner

@davidmigloz davidmigloz commented Apr 30, 2024

A RunnableMapInputStream allows you to map the input stream to a different stream of values.

Example:

final model = ChatOpenAI(
  apiKey: openAiApiKey,
  defaultOptions: ChatOpenAIOptions(
    responseFormat: ChatOpenAIResponseFormat(
      type: ChatOpenAIResponseFormatType.jsonObject,
    ),
  ),
);
final parser = JsonOutputParser<ChatResult>();
final mapper = Runnable.mapInputStream((Stream<Map<String, dynamic>> inputStream) async* {
  yield await inputStream.last;
});
final chain = model.pipe(parser).pipe(mapper);
final stream = chain.stream(
  PromptValue.string(
    'Output a list of the countries france, spain and japan and their '
        'populations in JSON format. Use a dict with an outer key of '
        '"countries" which contains a list of countries. '
        'Each country should have the key "name" and "population"',
  ),
);
await stream.forEach((final chunk) => print('$chunk|'));
// {countries: [{name: France, population: 65273511}, {name: Spain, population: 46754778}, {name: Japan, population: 126476461}]}|

@davidmigloz davidmigloz self-assigned this Apr 30, 2024
@davidmigloz davidmigloz added t:enhancement New feature or request c:lcel LangChain Expression Language p:langchain_core langchain_core package. labels Apr 30, 2024
@davidmigloz davidmigloz added this to the v0.6.0 milestone Apr 30, 2024
@davidmigloz davidmigloz merged commit a2b6bbb into main Apr 30, 2024
1 check passed
@davidmigloz davidmigloz deleted the map-input-stream branch April 30, 2024 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c:lcel LangChain Expression Language p:langchain_core langchain_core package. t:enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

1 participant