forked from SalamanderKrajza/ai_devs2_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.py
34 lines (26 loc) · 1.5 KB
/
08.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
#############################################################################
# ------------- Example 8 - chain of thoughts
#############################################################################
# Import necessary modules
from langchain.prompts import PromptTemplate
from langchain.chat_models.openai import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage
# Initialize the chat model
chat = ChatOpenAI(model_name='gpt-4')
# Get the answer using zero-shot prompt
zero_shot_response = chat.invoke([
SystemMessage('Answer the question ultra-briefly:'),
HumanMessage('100+48*62-9-100'),
])
print(zero_shot_response.content)
# Get the detailed answer with explanation
chain_of_thought_response = chat.invoke([
SystemMessage('Take a deep breath and answer the question by carefully explaining your logic step by step. Then add the separator: \n### and answer the question ultra-briefly with a single number:'),
HumanMessage('100+48*62-9-100'),
])
print(chain_of_thought_response.content)
# Extract the relevant part of the detailed answer
if isinstance(chain_of_thought_response.content, str) and isinstance(zero_shot_response.content, str):
chain_of_thought_result = chain_of_thought_response.content.split("\n###")[1]
print(f'Zero Shot: {int(zero_shot_response.content)}', 'Passed' if int(zero_shot_response.content) == 2967 else 'Failed 🙁')
print(f'Chain of Thought: {int(chain_of_thought_result)}', 'Passed' if int(chain_of_thought_result) == 2967 else 'Failed 🙁')