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json
|
| 3 |
+
from engine import UCGEngine,TOOLS
|
| 4 |
+
from groq import Groq
|
| 5 |
+
from components.ucg_g1 import UCG_GRAPH_1
|
| 6 |
+
|
| 7 |
+
client = Groq(api_key="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")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
runner=UCGEngine(UCG_GRAPH_1,TOOLS)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def UCGExecutor(operation: str,address: str, ):
|
| 16 |
+
"""
|
| 17 |
+
Execute a UGC graph operation for a given address.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
|
| 21 |
+
operation (str): The UGC operation to run (goal node in the graph).
|
| 22 |
+
address (str): Ethereum or Solana wallet address.
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
dict: The final state returned by the UGC engine.
|
| 26 |
+
"""
|
| 27 |
+
inputs = {"address": address}
|
| 28 |
+
|
| 29 |
+
# Assuming `runner` is an instance of UGCRunner with UGC_GRAPH_1 and TOOLS
|
| 30 |
+
result = runner.run(operation,inputs)
|
| 31 |
+
return result
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
available_functions = {
|
| 37 |
+
"UCGExecutor":UCGExecutor
|
| 38 |
+
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def run_conversation(user_prompt):
|
| 45 |
+
"""Run a conversation with tool calling"""
|
| 46 |
+
|
| 47 |
+
messages = [
|
| 48 |
+
|
| 49 |
+
{
|
| 50 |
+
"role":"system",
|
| 51 |
+
"content":'''
|
| 52 |
+
|
| 53 |
+
You are an AI assistant for the UGC Engine. Your job is to convert a user's natural language query into a valid tool call for the UGCExecutor.
|
| 54 |
+
|
| 55 |
+
The tool accepts only two fields:
|
| 56 |
+
1. "address": The wallet address (Ethereum hex or Solana Base58) to operate on.
|
| 57 |
+
2. "operation": The UGC operation/tool to run.
|
| 58 |
+
|
| 59 |
+
Valid operations/tools available:
|
| 60 |
+
- InfuraRPC: Fetch ETH balance for an Ethereum address.
|
| 61 |
+
- HeliusAPI: Fetch SOL balance for a Solana address.
|
| 62 |
+
- EthereumSigner: Produce a cryptographic signature for a given Ethereum address.
|
| 63 |
+
- SolanaSigner: Produce a cryptographic signature for a given Solana address.
|
| 64 |
+
|
| 65 |
+
Analyze the user query to determine:
|
| 66 |
+
- Which wallet address is mentioned.
|
| 67 |
+
- Which operation/tool the query implies.
|
| 68 |
+
Then call the tool with the required inputs
|
| 69 |
+
'''
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"role": "user",
|
| 73 |
+
"content": user_prompt,
|
| 74 |
+
}
|
| 75 |
+
]
|
| 76 |
+
|
| 77 |
+
# Define the tool schema
|
| 78 |
+
tools = [
|
| 79 |
+
|
| 80 |
+
{
|
| 81 |
+
"type": "function",
|
| 82 |
+
"function": {
|
| 83 |
+
"name": "UCGExecutor",
|
| 84 |
+
"description": "Execute a Unified Capabilities Graph operation for a given wallet address. The AI specifies the target operation, and the engine resolves the workflow automatically.",
|
| 85 |
+
"parameters": {
|
| 86 |
+
"type": "object",
|
| 87 |
+
"properties": {
|
| 88 |
+
|
| 89 |
+
"operation": {
|
| 90 |
+
"type": "string",
|
| 91 |
+
"description": "The name of the target operation/goal to run in the UGC graph (e.g., EthereumSigner, SolanaSigner, InfuraRPC, HeliusAPI)."
|
| 92 |
+
},
|
| 93 |
+
"address": {
|
| 94 |
+
"type": "string",
|
| 95 |
+
"description": "The wallet address to operate on (Ethereum hex or Solana Base58)."
|
| 96 |
+
},
|
| 97 |
+
|
| 98 |
+
},
|
| 99 |
+
"required": ["address", "operation"],
|
| 100 |
+
"additionalProperties": False
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
]
|
| 107 |
+
|
| 108 |
+
# Step 1: Make initial API call
|
| 109 |
+
response = client.chat.completions.create(
|
| 110 |
+
model="moonshotai/kimi-k2-instruct-0905",
|
| 111 |
+
messages=messages,
|
| 112 |
+
tools=tools,
|
| 113 |
+
tool_choice="required",
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
response_message = response.choices[0].message
|
| 117 |
+
tool_calls = response_message.tool_calls
|
| 118 |
+
|
| 119 |
+
# Step 2: Check if the model wants to call tools
|
| 120 |
+
if tool_calls:
|
| 121 |
+
|
| 122 |
+
# Add the assistant's response to conversation
|
| 123 |
+
messages.append(response_message)
|
| 124 |
+
|
| 125 |
+
# Step 3: Execute each tool call
|
| 126 |
+
for tool_call in tool_calls:
|
| 127 |
+
function_name = tool_call.function.name
|
| 128 |
+
function_to_call = available_functions[function_name]
|
| 129 |
+
function_args = json.loads(tool_call.function.arguments)
|
| 130 |
+
function_response = function_to_call(
|
| 131 |
+
operation=function_args.get("operation"),
|
| 132 |
+
address=function_args.get("address")
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Add tool response to conversation
|
| 136 |
+
messages.append({
|
| 137 |
+
"tool_call_id": tool_call.id,
|
| 138 |
+
"role": "tool",
|
| 139 |
+
"name": function_name,
|
| 140 |
+
"content": json.dumps(function_response ,indent=2),
|
| 141 |
+
})
|
| 142 |
+
|
| 143 |
+
# Step 4: Get final response from model
|
| 144 |
+
second_response = client.chat.completions.create(
|
| 145 |
+
model="moonshotai/kimi-k2-instruct-0905",
|
| 146 |
+
messages=messages
|
| 147 |
+
)
|
| 148 |
+
return second_response.choices[0].message.content
|
| 149 |
+
|
| 150 |
+
# If no tool calls, return the direct response
|
| 151 |
+
return response_message.content
|