| 1 | import json |
| 2 | from openai import OpenAI |
| 3 | |
| 4 | # Equipet assistant with tools: |
| 5 | tools = [ |
| 6 | { |
| 7 | "type": "function", |
| 8 | "function": { |
| 9 | "name": "run_cypher_query", |
| 10 | "description": "Run a Cypher query against the graph", |
| 11 | "parameters": { |
| 12 | "type": "object", |
| 13 | "properties": { |
| 14 | "query": { |
| 15 | "type": "string", |
| 16 | "description": "query to execute", |
| 17 | }, |
| 18 | }, |
| 19 | "required": ["query"], |
| 20 | }, |
| 21 | }, |
| 22 | } |
| 23 | ] |
| 24 | |
| 25 | client = OpenAI() |
| 26 | |
| 27 | def run_cypher_query(g, q): |
| 28 | return g.query(q).result_set |
| 29 | |
| 30 | # convert a schema into a string representation |
| 31 | def _graph_schema_to_prompt(s) -> str: |
| 32 | # Build a string description of graph schema |
| 33 | desc = "The knowladge graph schema is as follows:\n" |
| 34 | |
| 35 | #-------------------------------------------------------------------------- |
| 36 | # Describe labels |
| 37 | #-------------------------------------------------------------------------- |
| 38 | |
| 39 | # list labels |
| 40 | desc = desc + "The graph contains the following node labels:\n"; |
| 41 | for e in s.entities: |
| 42 | desc += e + "\n" |
| 43 | |
| 44 | # specify attributes associated with each entity |
| 45 | for e in s.entities: |
| 46 | e = s.entities[e] |
| 47 | |
| 48 | lbl_name = e.name |
| 49 | attributes = e.attributes |
| 50 | attr_count = len(attributes) |
| 51 | |
| 52 | if (attr_count == 0): |
| 53 | desc += f"The {lbl_name} label has no attributes\n" |
| 54 | else: |
| 55 | desc += f"The {lbl_name} label is associated with the following attribute(s):\n" |
| 56 | for attr in attributes: |
| 57 | attr_name = attr.name |
| 58 | attr_type = attr.type.__name__ |
| 59 | desc += f"{attr_name} which is of type {attr_type}\n" |
| 60 | |
| 61 | desc += "The graph contains the following relationship types:\n" |
| 62 | |
| 63 | #-------------------------------------------------------------------------- |
| 64 | # Describe relationships |
| 65 | #-------------------------------------------------------------------------- |
| 66 | |
| 67 | # list relations |
| 68 | for rel in s.relations: |
| 69 | rel = s.relations[rel] |
| 70 | |
| 71 | src = rel.src.name |
| 72 | relationship_type = rel.name |
| 73 | dest = rel.dest.name |
| 74 | desc += f"{relationship_type}\n" |
| 75 | desc += f"The {relationship_type} relationship connects the following labels:\n" |
| 76 | desc += f"{src} is connected via {relationship_type} to {dest}\n" |
| 77 | |
| 78 | desc += "This is the end of the knowladge graph schema description.\n" |
| 79 | |
| 80 | return desc; |
| 81 | |
| 82 | def run_conversation(g, question, messages, model) -> str: |
| 83 | # Step 1: send the conversation and available functions to the model |
| 84 | messages.append({"role": "user", "content": question}) |
| 85 | |
| 86 | response = client.chat.completions.create( |
| 87 | model=model, |
| 88 | messages=messages, |
| 89 | tools=tools, |
| 90 | tool_choice="auto", # auto is default, but we'll be explicit |
| 91 | ) |
| 92 | |
| 93 | response_message = response.choices[0].message |
| 94 | tool_calls = response_message.tool_calls |
| 95 | |
| 96 | # Step 2: check if the model wanted to call a function |
| 97 | if tool_calls: |
| 98 | # Step 3: call the function |
| 99 | # Note: the JSON response may not always be valid; be sure to handle errors |
| 100 | available_functions = { |
| 101 | "run_cypher_query": run_cypher_query, |
| 102 | } # only one function in this example, but you can have multiple |
| 103 | messages.append(response_message) # extend conversation with assistant's reply |
| 104 | # Step 4: send the info for each function call and function response to the model |
| 105 | for tool_call in tool_calls: |
| 106 | function_name = tool_call.function.name |
| 107 | function_to_call = available_functions[function_name] |
| 108 | function_args = json.loads(tool_call.function.arguments) |
| 109 | q = function_args.get("query") |
| 110 | print(f"OpenAI suggested query: {q}") |
| 111 | function_response = function_to_call(g, q) |
| 112 | messages.append( |
| 113 | { |
| 114 | "tool_call_id": tool_call.id, |
| 115 | "role": "tool", |
| 116 | "name": function_name, |
| 117 | "content": str(function_response), |
| 118 | } |
| 119 | ) # extend conversation with function response |
| 120 | second_response = client.chat.completions.create( |
| 121 | model=model, |
| 122 | messages=messages, |
| 123 | ) # get a new response from the model where it can see the function response |
| 124 | |
| 125 | return second_response.choices[0].message.content |
| 126 | |
| 127 | return None |
| 128 | |
| 129 | def query(kg, question:str, messages:list, model="gpt-4-1106-preview") -> str: |
| 130 | # Build system message |
| 131 | if len(messages) == 0: |
| 132 | # graph schema |
| 133 | schema_desc = _graph_schema_to_prompt(kg.schema) |
| 134 | messages.append( |
| 135 | {"role": "system", "content": f"""You are a graph database expert fluent in the Cypher query language |
| 136 | with access to the following knowledge graph: {schema_desc}"""}) |
| 137 | |
| 138 | # Get an answer |
| 139 | answer = run_conversation(kg.graph, question, messages, model) |
| 140 | |
| 141 | # Append answer to conversation |
| 142 | messages.append({"role": "assistant", "content": answer}) |
| 143 | |
| 144 | return answer |