Geminí

L’adaptador Gemini permet que els models Gemini de Google interactuïn amb Asset Core a través de declaracions de funció, reflectint la mateixa superfície d’eina que altres adaptadors.

Requisits previs

  • Daemons d’Asset Core en execució
  • Accés a l’API d’IA de Google (Gemini)
  • Python o Rust per a la integració

Pas 1 - Obtenir declaracions de funció

L’adaptador proporciona declaracions de funció compatibles amb Gemini:

use assetcore_adapters::gemini::GeminiToolDefinition;

let tools = GeminiToolDefinition::all();

Aquestes declaracions segueixen l’esquema de crida de funcions de Google.

Pas 2 - Definir funcions per a Gemini

Format d’eines per a l’API de Gemini:

Orientació addicional:

Tonalitat: formal Registre: tècnic

Context:

  • domini: infraestructura de blockchain
  • audiència: desenvolupadors i investigadors
  • propòsit: documentació tècnica

Restriccions:

  • Utilitzar un llenguatge neutre en termes de gènere
  • Preservar noms de marca
  • Mantenir l’exactitud tècnica

Preservar sense traducció:

  • code_blocks
  • inline_code
  • links
  • glossary_terms
  • mathematical_notation

Orientació del glossari:

Preservar aquests termes exactament: Asset Core, Asset‑Core, CSP, API, Docker, ECS, daemon, L1, L2, L3, OpenGraph, GitHub, Astro, TypeScript, JavaScript, ℤ, ℤ², ℤ³, ℝ², ℝ³, 0D, 1D, 2D, 3D.

from google.generativeai.types import FunctionDeclaration, Tool

commit_function = FunctionDeclaration(
    name="assetcore_commit",
    description="Submit a transaction to Asset Core",
    parameters={
        "type": "object",
        "properties": {
            "operations": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "op": {"type": "string"},
                        "args": {"type": "object"}
                    },
                    "required": ["op", "args"]
                },
                "description": "List of operations to execute"
            },
            "idempotency_key": {
                "type": "string",
                "description": "Optional deduplication key"
            }
        },
        "required": ["operations"]
    }
)

health_function = FunctionDeclaration(
    name="assetcore_write_health",
    description="Check Asset Core write daemon health",
    parameters={"type": "object", "properties": {}}
)

tools = Tool(function_declarations=[commit_function, health_function])

Pas 3 - Executar crides a funcions

Quan Gemini retorna una crida a la funció, executa-la contra Asset Core:

import httpx
import json

async def execute_function(name: str, args: dict) -> dict:
    if name == "assetcore_commit":
        async with httpx.AsyncClient() as client:
            response = await client.post(
                "http://localhost:8080/v1/commit",
                json=args
            )
            return response.json()

    elif name == "assetcore_write_health":
        async with httpx.AsyncClient() as client:
            response = await client.get(
                "http://localhost:8080/v1/health"
            )
            return response.json()

    else:
        return {"error": f"Unknown function: {name}"}

Pas 4 - Completar el cicle de conversa

Integrar-se amb l’API de Gemini:

import google.generativeai as genai

genai.configure(api_key="your-api-key")
model = genai.GenerativeModel(
    "gemini-pro",
    tools=[tools]
)

chat = model.start_chat()

response = chat.send_message(
    "Create a container with ID 1001"
)

# Check for function calls
if response.candidates[0].content.parts[0].function_call:
    fc = response.candidates[0].content.parts[0].function_call

    # Execute the function
    result = await execute_function(fc.name, dict(fc.args))

    # Send the result back
    response = chat.send_message(
        genai.protos.Content(
            parts=[
                genai.protos.Part(
                    function_response=genai.protos.FunctionResponse(
                        name=fc.name,
                        response={"result": result}
                    )
                )
            ]
        )
    )

    print(response.text)

Pas 5 - Utilitzeu l’executor Rust

Per a les aplicacions Rust:

use assetcore_adapters::gemini::GeminiToolExecutor;
use assetcore_adapters::http_client::DaemonClient;

let client = DaemonClient::new(
    "http://localhost:8080",
    "http://localhost:8081"
);

let executor = GeminiToolExecutor::new(client);

let result = executor.execute(
    "assetcore_commit",
    serde_json::json!({
        "operations": [
            {
                "op": "CreateContainer",
                "args": {
                    "container_id": 1001,
                    "kind": "Standard"
                }
            }
        ]
    })
).await?;

Resolució de problemes

Funció no reconeguda

Assegureu-vos que el nom de la funció coincideixi exactament. Gemini és sensible a les majúscules.

Arguments no vàlids

Gemini pot generar arguments que no coincideixen amb l’esquema. Afegiu validació abans de l’execució.

Múltiples crides a funcions

Gemini pot sol·licitar múltiples crides de funció. Executeu-les de manera seqüencial per a Asset Core.

Properes passes