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

Support combining multiple output parsers #618

Merged
merged 8 commits into from
Apr 11, 2023
Merged

Conversation

jmandel
Copy link
Contributor

@jmandel jmandel commented Apr 4, 2023

Not sure if this is a recommended approach, but I'm finding in my prompts that I want to cause outputs like:

{
  "system": "http://www.nlm.nih.gov/research/umls/rxnorm",
  "display": "hydroxychloroquine",
  "code": "5521",
}

New Query: ?system=loinc&display=medication
  
Final Grade: A

Justification: The code "5521" for "hydroxychloroquine" is the most appropriate code for the medication
mentioned in the clinical note. It is a valid RXNORM code and accurately represents the medication mentioned.

It's easier for me to create a JSONOutputParser and combined it with a couple of RegexOutpuParsers,and the logic for this combination can be abstracted into something like this "Combining" output parser.

@vercel
Copy link

vercel bot commented Apr 4, 2023

@jmandel is attempting to deploy a commit to the LangChain Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Apr 4, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-docs ⬜️ Ignored (Inspect) Visit Preview Apr 11, 2023 10:50am

@nfcampos
Copy link
Collaborator

nfcampos commented Apr 5, 2023

@jmandel thanks for this, could you provide an example of this in action?

@jmandel
Copy link
Contributor Author

jmandel commented Apr 5, 2023

@nfcampos sure thing -- here's how I'm using it currently, to parse output like in the example above:

const chain = new LLMChain({
  outputParser: new MultiOutputParser(
    new CodingsParser("codings"), // parses all JSON objects in output --> `Record<string, any>[]`
    new RegexParser(/Final Grade: (A|B|C)/i, ["grade"], "noGrade"),
    new RegexParser(/Justification: (.*)/i, ["rationale"], "noRationale"),
    new RegexParser(
      /New Query: \?system=(\S+)&display=(\S+)/i,
      ["newQuerySystem", "newQuery"],
      "noQuery"
    )
  ),
  prompt,
  llm
});

Copy link
Collaborator

@hwchase17 hwchase17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh this is pretty cool. i like this a lot - i can do something similar in the python version, and then we can release at the same time and make a cool announcement

i think all thats missing from this PR is an example to put in the docs

@jmandel jmandel marked this pull request as ready for review April 7, 2023 17:05
@jmandel
Copy link
Contributor Author

jmandel commented Apr 7, 2023

Added an example and updated the class name.

@nfcampos nfcampos added the lgtm PRs that are ready to be merged as-is label Apr 11, 2023
@nfcampos nfcampos merged commit 47539da into langchain-ai:main Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm PRs that are ready to be merged as-is
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants