-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmindmap_utils.py
More file actions
149 lines (127 loc) · 5.72 KB
/
mindmap_utils.py
File metadata and controls
149 lines (127 loc) · 5.72 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from pydantic.v1 import BaseModel, Field
from typing import List, Optional
from langchain.output_parsers import PydanticOutputParser
from langchain.prompts import PromptTemplate
from query_data import query_rag
from langchain_together import Together
import textwrap
import json
import os
together_api_key = "YOUR_API_KEY"
def wrap_text(text, width=20):
return '\n'.join(textwrap.wrap(text, width=width))
class InfoNode(BaseModel):
information: str = Field(description="Relevant information about the topic")
emoji: str = Field(description="A relevant emoji for this information")
class SubTopicNode(BaseModel):
subtopic: str = Field(description="A subtopic of the main topic")
emoji: str = Field(description="A relevant emoji for this subtopic")
children: List[InfoNode] = Field(description="A list of 2-3 information nodes for this subtopic")
class TopicNode(BaseModel):
topic: str = Field(description="The main topic of the node")
emoji: str = Field(description="A relevant emoji for this topic")
summary: str = Field(description="A brief summary of the topic")
children: List[SubTopicNode] = Field(description="A list of 2-3 subtopic nodes")
class MindMap(BaseModel):
central_topic: str = Field(description="The central topic of the mind map")
central_emoji: str = Field(description="A relevant emoji for the central topic")
topicNodes: List[TopicNode] = Field(description="A list of 3-4 main topic nodes")
def generate_mindmap(topic: str, chat_id: str) -> dict:
context, sources = query_rag(f"Provide detailed information about {topic}", chat_id)
model = Together(
model="meta-llama/Meta-Llama-3-8B-Instruct-Lite",
temperature=0.7,
max_tokens=2048,
top_k=50,
together_api_key=together_api_key
)
parser = PydanticOutputParser(pydantic_object=MindMap)
mindmap_prompt = PromptTemplate(
template="""Based on the following information about {topic}, generate a detailed mind map with the following structure:
- A central topic with an emoji
- 3-4 main topic nodes, each with:
- An emoji
- A brief summary
- 2-3 subtopic nodes, each with:
- An emoji
- 2-3 information nodes
Ensure all emojis are relevant to their respective topics or information.
DO NOT USE JSON references like $ref or #defs. Provide only the actual content for each field.
Information about {topic}:
{context}
{format_instructions}""",
input_variables=["topic", "context"],
partial_variables={"format_instructions": parser.get_format_instructions()}
)
try:
mindmap_output = model.invoke(mindmap_prompt.format(topic=topic, context=context))
mindmap = parser.parse(mindmap_output)
except ValueError as e:
if "$ref" in str(e) or "#defs" in str(e):
strict_prompt = PromptTemplate(
template="""Based on the following information about {topic}, generate a detailed mind map with the following structure:
- A central topic with an emoji
- 3-4 main topic nodes, each with:
- An emoji
- A brief summary
- 2-3 subtopic nodes, each with:
- An emoji
- 2-3 information nodes
Ensure all emojis are relevant to their respective topics or information.
DO NOT USE ANY JSON SCHEMA REFERENCES like '#defs' or '$ref'. Provide only the actual content for each field.
Information about {topic}:
{context}
{format_instructions}
If mentioned by mistake, still don't use $defs or $ref""",
input_variables=["topic", "context"],
partial_variables={"format_instructions": parser.get_format_instructions()}
)
mindmap_output = model.invoke(strict_prompt.format(topic=topic, context=context))
mindmap = parser.parse(mindmap_output)
else:
raise
network_data = convert_to_network_data(mindmap)
return network_data
def convert_to_network_data(mindmap: MindMap) -> dict:
nodes = []
edges = []
node_id = 1
# Central topic
nodes.append({
"id": node_id,
"label": f"{mindmap.central_emoji} {wrap_text(mindmap.central_topic)}",
"group": "central"
})
central_id = node_id
node_id += 1
for topic_node in mindmap.topicNodes:
topic_id = node_id
nodes.append({
"id": topic_id,
"label": f"{topic_node.emoji} {wrap_text(topic_node.topic)}\n{wrap_text(topic_node.summary)}",
"group": "topic"
})
edges.append({"from": central_id, "to": topic_id})
node_id += 1
for subtopic_node in topic_node.children:
subtopic_id = node_id
nodes.append({
"id": subtopic_id,
"label": f"{subtopic_node.emoji} {wrap_text(subtopic_node.subtopic)}",
"group": "subtopic"
})
edges.append({"from": topic_id, "to": subtopic_id})
node_id += 1
for info_node in subtopic_node.children:
nodes.append({
"id": node_id,
"label": f"{info_node.emoji} {wrap_text(info_node.information)}",
"group": "info",
"shape": "box",
"size": 30,
"font": {"size": 14},
"margin": 10
})
edges.append({"from": subtopic_id, "to": node_id})
node_id += 1
return {"nodes": nodes, "edges": edges}