-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput-parsers.py
54 lines (39 loc) · 1.66 KB
/
output-parsers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from dotenv import load_dotenv
load_dotenv()
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser, CommaSeparatedListOutputParser, JsonOutputParser
from langchain_core.pydantic_v1 import BaseModel, Field
model = ChatOpenAI(model="gpt-4-turbo", temperature=0.7)
def call_string_output_parser():
prompt = ChatPromptTemplate.from_messages([
("system", "tell me a joke about the following subject."),
("human", "{input}")
])
parser = StrOutputParser()
chain = prompt | model | parser
return chain.invoke({"input": "dog"})
# print(call _string_output_parser())
def call_list_output_parser():
prompt = ChatPromptTemplate.from_messages([
("system", "generate a list of 10 synonyms for the following word. Return the results as a comma seperated list."),
("human", "{input}")
])
parser = CommaSeparatedListOutputParser()
chain = prompt | model | parser
return chain.invoke({"input": "happy"})
def call_json_output_parser():
prompt = ChatPromptTemplate.from_messages([
("system", "extract information from the following phrase. \nFormatting Instructions: {format_instructions}"),
("human", "{phrase}")
])
class Person(BaseModel):
name: str = Field(description="The name of recipe")
ingredients: list = Field(description="ingredients")
parser = JsonOutputParser(pydantic_object=Person)
chain = prompt | model | parser
return chain.invoke({
"phrase": "the ingrediends for a Margarita are tequila, triple sec, and lime juice.",
"format_instructions": parser.get_format_instructions()
})
print(call_json_output_parser())