forked from kenliong/sophie_diary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew_diary_entry.py
73 lines (53 loc) · 2.27 KB
/
new_diary_entry.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
71
72
73
from typing import List, Optional
from langchain.output_parsers import PydanticOutputParser
from pydantic import BaseModel, Field
from utils.llm_utils import *
class DeepDiveConversationLabels(BaseModel):
emotions: List[str] = Field(
default_factory=lambda: [],
description="label the emotions that this person experienced. If this information is not found, output an empty list [].",
)
current_state: Optional[str] = Field(
default="",
description="Label the current state (or real outcome) that this person experienced. If this information is not found, output 'None'.",
)
desired_state: Optional[str] = Field(
default="",
description="Label the desired state (or desired outcome, expectation) that this person expected. If this information is not found, output 'None'",
)
def extract_info_from_conversation(chat_history):
parser = PydanticOutputParser(pydantic_object=DeepDiveConversationLabels)
prompt = f"""
The following are thoughts from a user, extract the following information.
{parser.get_format_instructions()}
```
{chat_history}
```
""".strip()
model = get_llm_instance()
output = get_completion(model, prompt)
parsed_output = parser.parse(output)
return parsed_output
class DiaryEntrySummary(BaseModel):
entry_title: str = Field(
description="Summarize this person's desired state with a goal of understanding this person's value. Turn this person's value into the title. Write this in first person perspective."
)
entry_summary: str = Field(
description="A 1 to 2 paragraph summary of the user's experience based on the conversation history between the user and an AI model. Write this in first person perspective."
)
def summarize_new_entry(chat_model):
chat_history = ""
for msg in chat_model.history[2:]:
chat_history += f"{msg.role}: {msg.parts[0].text} \n\n"
parser = PydanticOutputParser(pydantic_object=DiaryEntrySummary)
prompt = f"""
The following are thoughts from a user, extract the following information.
{parser.get_format_instructions()}
```
{chat_history}
```
""".strip()
model = get_llm_instance()
output = get_completion(model, prompt)
parsed_output = parser.parse(output)
return parsed_output