-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_ai_exec.py
More file actions
40 lines (35 loc) · 1.38 KB
/
open_ai_exec.py
File metadata and controls
40 lines (35 loc) · 1.38 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
import os
import uuid
from collections import defaultdict
from dotenv import load_dotenv
from neo4j import GraphDatabase
from neo4j_graphrag.retrievers import QdrantNeo4jRetriever
from openai import OpenAI
from pydantic import BaseModel
from qdrant_client import QdrantClient, models
client = OpenAI()
def openai_llm_parser(prompt):
completion = client.chat.completions.create(
model="gpt-4o-2024-08-06",
response_format={"type": "json_object"},
messages=[
{
"role": "system",
"content": """ You are a precise graph relationship extractor. Extract all
relationships from the text and format them as a JSON object
with this exact structure:
{
"graph": [
{"node": "Person/Entity",
"target_node": "Related Entity",
"relationship": "Type of Relationship"},
...more relationships...
]
}
Include ALL relationships mentioned in the text, including
implicit ones. Be thorough and precise. """,
},
{"role": "user", "content": prompt},
],
)
return GraphComponents.model_validate_json(completion.choices[0].message.content)