| 1 | import json |
| 2 | import time |
| 3 | import concurrent.futures |
| 4 | from openai import OpenAI |
| 5 | from rag_sdk.prompts import KNOWLEDGE_EXTRACTION_SYSTEM_PROMPT |
| 6 | from falkordb import FalkorDB |
| 7 | |
| 8 | g = None # graph |
| 9 | client = None # OpenAI client |
| 10 | |
| 11 | tools = [<TOOLS>] |
| 12 | |
| 13 | <FUNCTIONS> |
| 14 | |
| 15 | available_functions = <AVAILABLE_FUNCTIONS> |
| 16 | |
| 17 | def remove_none_values(args): |
| 18 | return {key: value for key, value in args.items() if value is not None} |
| 19 | |
| 20 | def create_assistant(client, assistant_name, instructions, model): |
| 21 | assistant = client.beta.assistants.create( |
| 22 | name = assistant_name, |
| 23 | instructions = instructions, |
| 24 | model = model, |
| 25 | tools = tools |
| 26 | ) |
| 27 | |
| 28 | return assistant |
| 29 | |
| 30 | def initiate_interaction(client, assistant, user_message): |
| 31 | run = client.beta.threads.create_and_run( |
| 32 | assistant_id=assistant.id, |
| 33 | thread={ "messages": [ {"role": "user", "content": user_message} ] } |
| 34 | ) |
| 35 | |
| 36 | return run |
| 37 | |
| 38 | def handle_run(client, run): |
| 39 | while True: |
| 40 | run = client.beta.threads.runs.retrieve(thread_id=run.thread_id, run_id=run.id) |
| 41 | |
| 42 | if run.status == "completed": |
| 43 | print("Run completed") |
| 44 | return |
| 45 | |
| 46 | if run.status == "expired": |
| 47 | print(f"Run expired") |
| 48 | return |
| 49 | |
| 50 | if run.status == "cancelling" or run.status == "cancelled" or run.status == "failed": |
| 51 | print(f"Run failed") |
| 52 | return |
| 53 | |
| 54 | # wait for run to complete |
| 55 | if run.status == "queued": |
| 56 | print("Run queued") |
| 57 | time.sleep(1) |
| 58 | continue |
| 59 | |
| 60 | if run.status == "in_progress": |
| 61 | last_step_id = None |
| 62 | print("Run in progress") |
| 63 | run_steps = [] |
| 64 | # if last_step_id is None: |
| 65 | # run_steps = client.beta.threads.runs.steps.list(thread_id=run.thread_id, run_id=run.id) |
| 66 | # else: |
| 67 | # run_steps = client.beta.threads.runs.steps.list(thread_id=run.thread_id, run_id=run.id, after=last_step_id) |
| 68 | run_steps = client.beta.threads.runs.steps.list(thread_id=run.thread_id, run_id=run.id) |
| 69 | |
| 70 | for step in run_steps.data: |
| 71 | if step.status == "in_progress": |
| 72 | step_details = step.step_details |
| 73 | if step_details.type == "tool_calls": |
| 74 | for tool_call in step_details.tool_calls: |
| 75 | try: |
| 76 | if tool_call.type == "function": |
| 77 | func = tool_call.function |
| 78 | func_name = func.name |
| 79 | args = func.arguments |
| 80 | args = json.loads(args) |
| 81 | print(f"Function: {func_name}, {args}") |
| 82 | f = available_functions[func_name] |
| 83 | f(args) |
| 84 | except: |
| 85 | print(f"tool_call: {tool_call}") |
| 86 | pass |
| 87 | |
| 88 | last_step_id = run_steps.last_id |
| 89 | |
| 90 | time.sleep(1) |
| 91 | continue |
| 92 | |
| 93 | if run.status == "requires_action": |
| 94 | print(f"Run requires action") |
| 95 | action = run.required_action |
| 96 | if action.type == "submit_tool_outputs": |
| 97 | tool_outputs = action.submit_tool_outputs |
| 98 | outputs = [] |
| 99 | for tool in tool_outputs.tool_calls: |
| 100 | tool_call_id = tool.id |
| 101 | tool_type = tool.type |
| 102 | func = tool.function |
| 103 | func_name = func.name |
| 104 | args = func.arguments |
| 105 | args = json.loads(args) |
| 106 | print(f"Function: {func_name}, {args}") |
| 107 | |
| 108 | f = available_functions[func_name] |
| 109 | f(args) |
| 110 | |
| 111 | outputs.append({ "tool_call_id": tool_call_id, "output": "OK" }) |
| 112 | |
| 113 | # respond to action |
| 114 | run = client.beta.threads.runs.submit_tool_outputs( |
| 115 | thread_id=run.thread_id, |
| 116 | run_id=run.id, |
| 117 | tool_outputs=outputs |
| 118 | ) |
| 119 | |
| 120 | continue |
| 121 | |
| 122 | def process_source(client, assistant, src): |
| 123 | text = src.load()[:32000] |
| 124 | user_message = f"Extracts entities and relations from the following text:\n {text}" |
| 125 | |
| 126 | run = initiate_interaction(client, assistant, user_message) |
| 127 | |
| 128 | handle_run(client, run) |
| 129 | |
| 130 | client.beta.threads.delete(run.thread_id) |
| 131 | |
| 132 | def build_graph_from_sources(kg, client, srcs:set): |
| 133 | global g |
| 134 | g = kg.graph |
| 135 | |
| 136 | assistant = create_assistant(client, kg.name, KNOWLEDGE_EXTRACTION_SYSTEM_PROMPT, model=kg.model) |
| 137 | |
| 138 | # Create thread pool |
| 139 | with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor: |
| 140 | tasks = [] |
| 141 | # extract entities and relationships from each page |
| 142 | for src in srcs: |
| 143 | task = executor.submit(process_source, client, assistant, src) |
| 144 | |
| 145 | # Wait for all tasks to complete |
| 146 | concurrent.futures.wait(tasks) |
| 147 | |
| 148 | print("Done") |