forked from i-am-alice/2nd-devs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
C03L05_people.py
70 lines (58 loc) · 2.54 KB
/
C03L05_people.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
60
61
62
63
64
65
66
67
68
69
70
import sys
sys.path.append(r'..')
from task_handler import get_task_token, get_task_info_from_token, send_answer_by_task_token, apikey
# --------------------------------------------------------------
# Get task data
# --------------------------------------------------------------
import json
task_token = get_task_token(taskname='people', apikey=apikey)
task_data = get_task_info_from_token(task_token)
print(json.dumps(task_data, indent=4, ensure_ascii=False))
question = task_data['question']
# --------------------------------------------------------------
# Get the data
# --------------------------------------------------------------
import requests
url = "https://tasks.aidevs.pl/data/people.json"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'}
response = requests.get(url, headers=headers)
content = json.loads(response.text)
# --------------------------------------------------------------
# Convert it into pandas (mostly because i am used to it)
# --------------------------------------------------------------
import pandas as pd
df = pd.DataFrame(content)
df['osoba'] = df['imie'] + " "+ df['nazwisko']
df
# --------------------------------------------------------------
# Get person info
# --------------------------------------------------------------
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage, AIMessage
chat = ChatOpenAI()
response = chat.invoke(input=
[
SystemMessage("pełna forma imienia i nazwisko, nie odpowiadaj na pytanie użytkownika, podaj tylko imię i nazwisko osoby z pytania"),
HumanMessage(question)
]
)
person_info = df.query(f"osoba=='{response.content}'").to_json()
# --------------------------------------------------------------
# Get answer to question
# --------------------------------------------------------------
response = chat.invoke(input=
[
SystemMessage(f"###CONTEXT###\n{person_info}"),
HumanMessage(question)
]
)
print("Pytanie: ", question)
print("Odpowiedź: ", response.content)
# --------------------------------------------------------------
# Prepare answer
# --------------------------------------------------------------
data = {"answer": response.content}
# --------------------------------------------------------------
# send answer
# --------------------------------------------------------------
response = send_answer_by_task_token(task_token, data)