mirror of
https://github.com/hwchase17/langchain
synced 2024-11-04 06:00:26 +00:00
767523f364
![Screenshot 2024-02-23 at 7 48 03 PM](https://github.com/langchain-ai/langchain/assets/22008038/e5540c4d-0020-4ece-869f-ae19db2a1f3f)
41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
from typing import List, Optional
|
|
|
|
from langchain.utils.openai_functions import convert_pydantic_to_openai_function
|
|
from langchain_core.output_parsers.openai_functions import JsonKeyOutputFunctionsParser
|
|
from langchain_core.prompts import ChatPromptTemplate
|
|
from langchain_core.pydantic_v1 import BaseModel
|
|
from langchain_experimental.llms.anthropic_functions import AnthropicFunctions
|
|
|
|
template = """A article will be passed to you. Extract from it all papers that are mentioned by this article.
|
|
|
|
Do not extract the name of the article itself. If no papers are mentioned that's fine - you don't need to extract any! Just return an empty list.
|
|
|
|
Do not make up or guess ANY extra information. Only extract what exactly is in the text.""" # noqa: E501
|
|
|
|
prompt = ChatPromptTemplate.from_messages([("system", template), ("human", "{input}")])
|
|
|
|
|
|
# Function output schema
|
|
class Paper(BaseModel):
|
|
"""Information about papers mentioned."""
|
|
|
|
title: str
|
|
author: Optional[str]
|
|
|
|
|
|
class Info(BaseModel):
|
|
"""Information to extract"""
|
|
|
|
papers: List[Paper]
|
|
|
|
|
|
# Function definition
|
|
model = AnthropicFunctions()
|
|
function = [convert_pydantic_to_openai_function(Info)]
|
|
|
|
chain = (
|
|
prompt
|
|
| model.bind(functions=function, function_call={"name": "Info"})
|
|
| JsonKeyOutputFunctionsParser(key_name="papers")
|
|
)
|