-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy path004-chains.py
More file actions
54 lines (37 loc) · 1.1 KB
/
004-chains.py
File metadata and controls
54 lines (37 loc) · 1.1 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
openai_api_key = os.environ["OPENAI_API_KEY"]
from langchain_openai import OpenAI
llmModel = OpenAI()
from langchain_openai import ChatOpenAI
chatModel = ChatOpenAI(model="gpt-3.5-turbo-0125")
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.prompts import FewShotChatMessagePromptTemplate
examples = [
{"input": "hi!", "output": "¡hola!"},
{"input": "bye!", "output": "¡adiós!"},
]
example_prompt = ChatPromptTemplate.from_messages(
[
("human", "{input}"),
("ai", "{output}"),
]
)
few_shot_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=examples,
)
final_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are an English-Spanish translator."),
few_shot_prompt,
("human", "{input}"),
]
)
chain = final_prompt | chatModel
response = chain.invoke({"input": "Who was JFK?"})
print("\n----------\n")
print("Translate: Who was JFK?")
print(response.content)
print("\n----------\n")