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(output-parsers): Add streaming support to OutputFunctionsParsers #194

Merged
merged 1 commit into from
Nov 6, 2023

Conversation

davidmigloz
Copy link
Owner

@davidmigloz davidmigloz commented Nov 6, 2023

Example:

const function = ChatFunction(
  name: 'joke',
  description: 'A joke',
  parameters: {
    'type': 'object',
    'properties': {
      'setup': {
        'type': 'string',
        'description': 'The setup for the joke',
      },
      'punchline': {
        'type': 'string',
        'description': 'The punchline to the joke',
      },
    },
    'required': ['location', 'punchline'],
  },
);
final promptTemplate = ChatPromptTemplate.fromTemplate(
  'tell me a long joke about {foo}',
);
final chat = ChatOpenAI(apiKey: openaiApiKey, temperature: 0).bind(
  ChatOpenAIOptions(
    functions: const [function],
    functionCall: ChatFunctionCall.forced(functionName: 'joke'),
  ),
);
final jsonOutputParser = JsonOutputFunctionsParser();
final chain = promptTemplate.pipe(chat).pipe(jsonOutputParser);

final stream = chain.stream({'foo': 'bears'});

await for (final res in stream) {
  print(res);
}

will print:

{}
{setup: }
{setup: Why}
{setup: Why don}
{setup: Why don't}
{setup: Why don't bears}
{setup: Why don't bears like}
{setup: Why don't bears like fast}
{setup: Why don't bears like fast food}
{setup: Why don't bears like fast food?, punchline: }
{setup: Why don't bears like fast food?, punchline: Because}
{setup: Why don't bears like fast food?, punchline: Because they}
{setup: Why don't bears like fast food?, punchline: Because they can}
{setup: Why don't bears like fast food?, punchline: Because they can't}
{setup: Why don't bears like fast food?, punchline: Because they can't catch}
{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!}

@davidmigloz davidmigloz self-assigned this Nov 6, 2023
@davidmigloz davidmigloz added t:enhancement New feature or request c:output-parsers Output parsers. labels Nov 6, 2023
@davidmigloz davidmigloz added this to the v0.0.15 milestone Nov 6, 2023
@davidmigloz davidmigloz merged commit 8b4e6a1 into main Nov 6, 2023
@davidmigloz davidmigloz deleted the streaming branch November 6, 2023 21:02
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:output-parsers Output parsers. t:enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant