LangChain.ScrapeChain (langchainex v0.2.3)
ScrapeChain is a wrapper around a special type of Chain that requires 'input_schema' and 'input_text' in its input_variables and combines it with an output_parser. Once you define that chain, you can have the chain 'scrape' a text and return the formatted output in virtually any form.
Link to this section Summary
Functions
Creates a new ScrapeChain struct with the given chain, input_schema, and output_parser, you set up a scrapeChain with an input_schema and an output_parser, then you can ask it with whatever text you want.
default passthrough parser. 'result' will be a string so it is up to you to transform it into a native elixir structure or whatever you want.
Executes the scrapechain and returns the parsed result, can be called against the schema you defined when you made the chain, or you can override that schema: result = LangChain.ScrapeChain.scrape(schema_chain, "John Doe is 30 years old") # result will be %{ name: "John Doe", age: 30, birthdate: "1987-01-01"} # override the default schema input_variables = %{
Link to this section Functions
new(chain, input_schema, output_parser \\ &LangChain.ScrapeChain.no_parse/1)
Creates a new ScrapeChain struct with the given chain, input_schema, and output_parser, you set up a scrapeChain with an input_schema and an output_parser, then you can ask it with whatever text you want.
example
Example:
create a chat to extract data:
chat = Chat.add_prompt_templates(%Chat{}, [ %{
role: "user",
prompt: %PromptTemplate{
template: "Schema: """
<%= input_schema %>
"""
Text: """
<%= input_text %>
"" Extract the data from Text according to Schema and return it in <%= output_format %> format.
Format any datetime fields using ISO8601 standard.
"
}
} ])
create a ChainLink with the chat and parser function
chain_link = %ChainLink{ name: "schema_extractor", input: chat, output_parser: &schema_parser/2 } chain = %Chain{links: [chain_link]} input_schema = "{ name: String, age: Number, birthdate: Date }" schema_chain = LangChain.ScrapeChain.new(chain, input_schema)
no_parse(result)
default passthrough parser. 'result' will be a string so it is up to you to transform it into a native elixir structure or whatever you want.
scrape(scrape_chain, llm_pid, input_variables)
Executes the scrapechain and returns the parsed result, can be called against the schema you defined when you made the chain, or you can override that schema: result = LangChain.ScrapeChain.scrape(schema_chain, "John Doe is 30 years old") # result will be %{ name: "John Doe", age: 30, birthdate: "1987-01-01"} # override the default schema input_variables = %{
input_text: "John Doe is 30 years old.",
input_schema: "{ firstName: String, lastName: String, age: Number, birthdate: Date }"
} alt_result = LangChain.ScrapeChain.scrape(schema_chain, input_variables) # alt_result will be %{ firstName: "John", lastName: "Doe", age: 30, birthdate: "1987-01-01"}