Spaces:
Sleeping
Sleeping
Commit
·
b31a1e4
1
Parent(s):
b79f8d4
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import openai
|
|
| 3 |
import json
|
| 4 |
from graphviz import Digraph
|
| 5 |
import base64
|
| 6 |
-
from io import BytesIO
|
| 7 |
from PIL import Image
|
| 8 |
|
| 9 |
def generate_knowledge_graph(api_key, user_input):
|
|
@@ -11,24 +10,38 @@ def generate_knowledge_graph(api_key, user_input):
|
|
| 11 |
openai.api_key = api_key
|
| 12 |
|
| 13 |
print("Making API call to OpenAI...")
|
| 14 |
-
completion = openai.
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
print("Received response from OpenAI.")
|
| 21 |
-
response_data = completion.choices[0]
|
| 22 |
print(f"Response data: {response_data}")
|
| 23 |
|
| 24 |
-
# For demonstration, let's assume the response_data is a JSON string that can be converted to a dictionary.
|
| 25 |
-
# You'll need to write code to interpret the text-based response to generate this dictionary.
|
| 26 |
print("Converting response to JSON...")
|
| 27 |
-
|
| 28 |
-
response_dict = json.loads(response_data)
|
| 29 |
-
except json.JSONDecodeError:
|
| 30 |
-
print("Failed to decode JSON. Using empty dictionary as a fallback.")
|
| 31 |
-
response_dict = {}
|
| 32 |
|
| 33 |
print("Generating knowledge graph using Graphviz...")
|
| 34 |
dot = Digraph(comment="Knowledge Graph")
|
|
|
|
| 3 |
import json
|
| 4 |
from graphviz import Digraph
|
| 5 |
import base64
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
def generate_knowledge_graph(api_key, user_input):
|
|
|
|
| 10 |
openai.api_key = api_key
|
| 11 |
|
| 12 |
print("Making API call to OpenAI...")
|
| 13 |
+
completion = openai.ChatCompletion.create(
|
| 14 |
+
model="gpt-3.5-turbo-16k",
|
| 15 |
+
messages=[
|
| 16 |
+
{
|
| 17 |
+
"role": "user",
|
| 18 |
+
"content": f"Help me understand following by describing as a detailed knowledge graph: {user_input}",
|
| 19 |
+
}
|
| 20 |
+
],
|
| 21 |
+
functions=[
|
| 22 |
+
{
|
| 23 |
+
"name": "knowledge_graph",
|
| 24 |
+
"description": "Generate a knowledge graph with entities and relationships.",
|
| 25 |
+
"parameters": {
|
| 26 |
+
"type": "object",
|
| 27 |
+
"properties": {
|
| 28 |
+
"metadata": {"type": "object"},
|
| 29 |
+
"nodes": {"type": "array"},
|
| 30 |
+
"edges": {"type": "array"}
|
| 31 |
+
},
|
| 32 |
+
"required": ["nodes", "edges"]
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
],
|
| 36 |
+
function_call={"name": "knowledge_graph"}
|
| 37 |
)
|
| 38 |
|
| 39 |
print("Received response from OpenAI.")
|
| 40 |
+
response_data = completion.choices[0]["message"]["function_call"]["arguments"]
|
| 41 |
print(f"Response data: {response_data}")
|
| 42 |
|
|
|
|
|
|
|
| 43 |
print("Converting response to JSON...")
|
| 44 |
+
response_dict = json.loads(response_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
print("Generating knowledge graph using Graphviz...")
|
| 47 |
dot = Digraph(comment="Knowledge Graph")
|