Elffuss BraidHub

kikocis/GraphRAG-SDK public

423ab603a5a9 · 4.6 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'] = r.name
40 tool['function']['description'] = "Form a connection 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 tool['function']['parameters']['properties'][attr.name] = {}
53 tool['function']['parameters']['properties'][attr.name]['type'] = type_mapping(attr.type)
54 if attr.desc is not None:
55 tool['function']['parameters']['properties'][attr.name]['description'] = attr.desc
56 else:
57 tool['function']['parameters']['properties'][attr.name]['description'] = src.name + "'s " + attr.name
58 required.append(attr.name)
59
60 # process edge's destination
61 # see if dest node has a unique attribute
62 attrs = dest.unique_attributes()
63 for attr in attrs:
64 tool['function']['parameters']['properties'][attr.name] = {}
65 tool['function']['parameters']['properties'][attr.name]['type'] = type_mapping(attr.type)
66 if attr.desc is not None:
67 tool['function']['parameters']['properties'][attr.name]['description'] = attr.desc
68 else:
69 tool['function']['parameters']['properties'][attr.name]['description'] = dest.name + "'s " + attr.name
70 required.append(attr.name)
71
72 tool['function']['parameters']['required'] = required
73
74 return tool
75
76def entity_to_tool(e:Entity):
77 # {
78 # "type": "function",
79 # "function": {
80 # "name": "directed",
81 # "description": "Form a connection between a director and a movie",
82 # "parameters": {
83 # "type": "object",
84 # "properties": {
85 # "src": {"type": "string", "description": "director's name"},
86 # "dest": {"type": "string", "description": "movie's title"}
87 # },
88 # "required": ["src", "dest"]
89 # }
90 # }
91 # }
92
93 tool = {}
94 tool['type'] = "function"
95 tool['function'] = {}
96
97 tool['function']['name'] = "Create_" + e.name
98 tool['function']['description'] = "Create a new " + e.name
99
100 tool['function']['parameters'] = {}
101 tool['function']['parameters']['type'] = "object"
102 tool['function']['parameters']['properties'] = {}
103
104 unique_attributes = []
105 for attr in e.attributes:
106 attribute_name = attr.name
107 attribute_type = type_mapping(attr.type)
108 attribute_unique = attr.unique
109
110 tool['function']['parameters']['properties'][attribute_name] = {}
111 tool['function']['parameters']['properties'][attribute_name]['type'] = attribute_type
112 if attr.desc is None:
113 tool['function']['parameters']['properties'][attribute_name]['description'] = f"{e.name}'s {attr.name}"
114 else:
115 tool['function']['parameters']['properties'][attribute_name]['description'] = attr.desc
116
117 if attribute_unique:
118 unique_attributes.append(attribute_name)
119
120 tool['function']['parameters']['required'] = unique_attributes
121
122 return tool
123
124# Create OpenAI tools from schema
125def schema_to_tools(s:Schema):
126 if not isinstance(s, Schema):
127 raise Exception("Invalid argument, expecting a Schema object")
128
129 tools = []
130 for e in s.entities:
131 tool = entity_to_tool(s.entities[e])
132 tools.append(tool)
133
134 for r in s.relations:
135 tool = relation_to_tool(s.relations[r])
136 tools.append(tool)
137
138 return tools