-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenAI_Langchain.py
More file actions
36 lines (28 loc) · 1.07 KB
/
Copy pathOpenAI_Langchain.py
File metadata and controls
36 lines (28 loc) · 1.07 KB
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
import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from typing import Optional
from pydantic import BaseModel, Field
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
class Dog(BaseModel):
"""Identifying information about a dog."""
name: str = Field(..., description="The dog's name")
color: str = Field(..., description="The dog's color")
fav_food: Optional[str] = Field(None, description="The dog's favorite food")
llm = ChatOpenAI(
model="gpt-3.5-turbo",
temperature=0,
max_tokens=None,
timeout=None,
api_key=OPENAI_API_KEY
)
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a world class algorithm for extracting information in structured formats."),
("human", "Use the given format to extract information from the following input: {input}"),
("human", "Tip: Make sure to answer in the correct format"),
]
)
chain = prompt | llm.with_structured_output(Dog)
res = chain.invoke({"input": "Harry was a chubby brown beagle who loved chicken"})
print(res)