langchain/templates/extraction-openai-functions/extraction_openai_functions/chain.py

48 lines
1.4 KiB
Python
Raw Normal View History

2023-10-27 02:44:30 +00:00
import json
from typing import List, Optional
2023-10-27 02:44:30 +00:00
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
2023-10-27 02:44:30 +00:00
from langchain.pydantic_v1 import BaseModel
from langchain.utils.openai_functions import convert_pydantic_to_openai_function
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.
2023-10-27 02:44:30 +00:00
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."""
2023-10-27 02:44:30 +00:00
title: str
author: Optional[str]
class Info(BaseModel):
"""Information to extract"""
2023-10-27 02:44:30 +00:00
papers: List[Paper]
2023-10-27 02:44:30 +00:00
# Function definition
model = ChatOpenAI()
function = [convert_pydantic_to_openai_function(Info)]
2023-10-27 02:44:30 +00:00
chain = (
prompt
| model.bind(functions=function, function_call={"name": "Info"})
| (
lambda x: json.loads(x.additional_kwargs["function_call"]["arguments"])[
"papers"
]
)
)
# chain = prompt | model.bind(
# functions=function, function_call={"name": "Info"}
# ) | JsonKeyOutputFunctionsParser(key_name="papers")