forked from i-am-alice/2nd-devs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
C02L02_inprompt.py
60 lines (47 loc) · 1.85 KB
/
C02L02_inprompt.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
55
56
57
58
59
import sys
sys.path.append(r'..')
from task_handler import get_task_token, get_task_info_from_token, send_answer_by_task_token, apikey
from dotenv import load_dotenv
load_dotenv()
# --------------------------------------------------------------
# Get task data
# --------------------------------------------------------------
task_token = get_task_token(taskname='inprompt', apikey=apikey)
task_data = get_task_info_from_token(task_token)
# --------------------------------------------------------------
# Prepare answer
# --------------------------------------------------------------
question = task_data['question']
print(question)
import pandas as pd
df = pd.DataFrame({'inputs':task_data['input']})
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage
model = ChatOpenAI()
name_to_filter_dataset = model([
SystemMessage(content="""
User will provide some question in polish.
Respond with name of person described in probided question.
Return only the name and nothing else.
"""),
HumanMessage(content=f"Question: {question}")
]).content
print(name_to_filter_dataset)
filtered_df = df.query(f'inputs.str.contains("{name_to_filter_dataset}")')
filtered_df.inputs.to_list()
answer = model([
SystemMessage(content=f"""
User will provide some question in polish.
You should respond to his question also in polish.
To provide answer use context below (and only context).
### context:
{filtered_df.inputs.to_list()}
"""),
HumanMessage(content=f"Question: {question}")
]).content
print(answer)
data = {"answer": answer}
# --------------------------------------------------------------
# send answer
# --------------------------------------------------------------
response = send_answer_by_task_token(task_token, data)