-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
186 lines (174 loc) · 7.64 KB
/
main.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import json
import openai
import logging
from bs4 import BeautifulSoup
import re
from neo4j import GraphDatabase
# Set your OpenAI API key
openai.api_key = ""
response_data = ""
# If Neo4j credentials are set, then Neo4j is used to store information
neo4j_username = os.environ.get("NEO4J_USERNAME")
neo4j_password = os.environ.get("NEO4J_PASSWORD")
neo4j_url = os.environ.get("NEO4J_URL")
neo4j_driver = None
if neo4j_username and neo4j_password and neo4j_url:
neo4j_driver = GraphDatabase.driver(
neo4j_url, auth=(neo4j_username, neo4j_password))
# Configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()
# Define your AWS Lambda handler function
def lambda_handler(event, context):
print("hiii squidward")
try:
global response_data
request_data = json.loads(event['body'])
print("request: " + str(request_data))
user_input = request_data.get("user_input", "")
if not user_input:
return {
"statusCode": 400,
"body": json.dumps({"error": "No input provided"})
}
print("Starting OpenAI call")
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-16k",
messages=[
{
"role": "user",
"content": f"Help me understand following by describing as a detailed knowledge graph: {user_input}"
}
],
functions=[
{
"name": "knowledge_graph",
"description": "Generate a knowledge graph with entities and relationships. Make the label the relationship between the nodes like java to jre would be 'runs on', etc.. Use the colors to help "
"differentiate between different node or edge types/categories to differentiate between nodes. Always provide light "
"pastel colors that work well with black font. Please try to use a different color for different nodes. And if you can find a wiki for the "
"concept, share the full link, empty string otherwise.",
"parameters": {
"type": "object",
"properties": {
"metadata": {
"type": "object",
"properties": {
"createdDate": {"type": "string"},
"lastUpdated": {"type": "string"},
"description": {"type": "string"}
}
},
"nodes": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {"type": "string"},
"label": {"type": "string"},
"type": {"type": "string"},
"color": {"type": "string"}, # Added color property
"wiki": {"type": "string"}, # Added wiki property
"properties": {
"type": "object",
"description": "Additional attributes for the node"
}
},
"required": [
"id",
"label",
"type",
"color",
"wiki"
] # Added color to required
}
},
"edges": {
"type": "array",
"items": {
"type": "object",
"properties": {
"from": {"type": "string"},
"to": {"type": "string"},
"relationship": {"type": "string"},
"direction": {"type": "string"},
"color": {"type": "string"}, # Added color property
"properties": {
"type": "object",
"description": "Additional attributes for the edge"
}
},
"required": [
"from",
"to",
"relationship",
"color"
] # Added color to required
}
}
},
"required": ["nodes", "edges"]
}
}
],
function_call={"name": "knowledge_graph"}
)
response_data = completion.choices[0]["message"]["function_call"]["arguments"]
# Remove trailing commas
response_data = re.sub(r',\s*}', '}', response_data)
response_data = re.sub(r',\s*]', ']', response_data)
print(response_data)
# Process graph data using the response_data
if neo4j_driver:
nodes, _, _ = neo4j_driver.execute_query("""
MATCH (n)
WITH collect(
{data: {id: n.id, label: n.label, color: n.color, wiki: n.wiki}}) AS node
RETURN node
""")
print()
nodes = [el['node'] for el in nodes][0]
edges, _, _ = neo4j_driver.execute_query("""
MATCH (s)-[r]->(t)
WITH collect(
{data: {source: s.id, target: t.id, label:r.type, color: r.color, wiki: r.wiki}}
) AS rel
RETURN rel
""")
edges = [el['rel'] for el in edges][0]
else:
print(response_data)
response_dict = json.loads(response_data)
# Assume response_data is global or passed appropriately
nodes = [
{
"data": {
"id": node["id"],
"label": node["label"],
"color": node.get("color", "defaultColor"),
"wiki": node.get("wiki", ""),
}
}
for node in response_dict["nodes"]
]
edges = [
{
"data": {
"source": edge["from"],
"target": edge["to"],
"label": edge["relationship"],
"color": edge.get("color", "defaultColor"),
}
}
for edge in response_dict["edges"]
]
return {
"statusCode": 200,
"body": json.dumps({"elements": {"nodes": nodes, "edges": edges}})
}
except Exception as e:
logger.error(str(e))
return {
"statusCode": 500,
"body": json.dumps({"error": "Internal server error"})
}