Elffuss BraidHub

kikocis/GraphRAG-SDK public

ca14f368bce9 · 4.7 KiBRawPermalink
1from .entity import Entity
2from .schema import Schema
3from .relation import Relation
4
5def type_mapping(t) -> str:
6 if t not in [str, int, float, bool]:
7 raise Exception(f"Unsupported type: {t.__name__}")
8
9 if t == str:
10 return "string"
11 elif t == bool:
12 return "boolean"
13 else:
14 return "number"
15
16def relation_to_tool(r:Relation):
17 # {
18 # "type": "function",
19 # "function": {
20 # "name": "directed",
21 # "description": "Form a connection between a director and a movie",
22 # "parameters": {
23 # "type": "object",
24 # "properties": {
25 # "src": {"type": "string", "description": "director's name"},
26 # "dest": {"type": "string", "description": "movie's title"}
27 # },
28 # "required": ["src", "dest"]
29 # }
30 # }
31 # }
32
33 src = r.src
34 dest = r.dest
35
36 tool = {}
37 tool['type'] = "function"
38 tool['function'] = {}
39 tool['function']['name'] = f"{src.name}_{r.name}_{dest.name}"
40 tool['function']['description'] = f"Form a connection of type {r.name} between {src.name} and {dest.name}"
41
42 tool['function']['parameters'] = {}
43 tool['function']['parameters']['type'] = "object"
44 tool['function']['parameters']['properties'] = {}
45
46 required = []
47
48 # process edge's src
49 # see if src node has a unique attribute
50 attrs = src.unique_attributes()
51 for attr in attrs:
52 property_name = f"{src.name}_{attr.name}"
53 tool['function']['parameters']['properties'][property_name] = {}
54 tool['function']['parameters']['properties'][property_name]['type'] = type_mapping(attr.type)
55 if attr.desc is not None:
56 tool['function']['parameters']['properties'][property_name]['description'] = attr.desc
57 else:
58 tool['function']['parameters']['properties'][property_name]['description'] = src.name + "'s " + attr.name
59 required.append(property_name)
60
61 # process edge's destination
62 # see if dest node has a unique attribute
63 attrs = dest.unique_attributes()
64 for attr in attrs:
65 property_name = f"{dest.name}_{attr.name}"
66 tool['function']['parameters']['properties'][property_name] = {}
67 tool['function']['parameters']['properties'][property_name]['type'] = type_mapping(attr.type)
68 if attr.desc is not None:
69 tool['function']['parameters']['properties'][property_name]['description'] = attr.desc
70 else:
71 tool['function']['parameters']['properties'][property_name]['description'] = dest.name + "'s " + attr.name
72 required.append(property_name)
73
74 tool['function']['parameters']['required'] = required
75
76 return tool
77
78def entity_to_tool(e:Entity):
79 # {
80 # "type": "function",
81 # "function": {
82 # "name": "directed",
83 # "description": "Form a connection between a director and a movie",
84 # "parameters": {
85 # "type": "object",
86 # "properties": {
87 # "src": {"type": "string", "description": "director's name"},
88 # "dest": {"type": "string", "description": "movie's title"}
89 # },
90 # "required": ["src", "dest"]
91 # }
92 # }
93 # }
94
95 tool = {}
96 tool['type'] = "function"
97 tool['function'] = {}
98
99 tool['function']['name'] = "Create_" + e.name
100 tool['function']['description'] = "Create a new " + e.name
101
102 tool['function']['parameters'] = {}
103 tool['function']['parameters']['type'] = "object"
104 tool['function']['parameters']['properties'] = {}
105
106 unique_attributes = []
107 for attr in e.attributes:
108 attribute_name = attr.name
109 attribute_type = type_mapping(attr.type)
110 attribute_unique = attr.unique
111
112 tool['function']['parameters']['properties'][attribute_name] = {}
113 tool['function']['parameters']['properties'][attribute_name]['type'] = attribute_type
114 if attr.desc is None:
115 tool['function']['parameters']['properties'][attribute_name]['description'] = f"{e.name}'s {attr.name}"
116 else:
117 tool['function']['parameters']['properties'][attribute_name]['description'] = attr.desc
118
119 if attribute_unique:
120 unique_attributes.append(attribute_name)
121
122 tool['function']['parameters']['required'] = unique_attributes
123
124 return tool
125
126# Create OpenAI tools from schema
127def schema_to_tools(s:Schema):
128 if not isinstance(s, Schema):
129 raise Exception("Invalid argument, expecting a Schema object")
130
131 tools = []
132 for e in s.entities:
133 tool = entity_to_tool(e)
134 tools.append(tool)
135
136 for r in s.relations:
137 tool = relation_to_tool(r)
138 tools.append(tool)
139
140 return tools