Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
linenumberstrue
@Slf4j
@AiToolComponent
public class ExampleAiTool
    implements AiTool
{

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    private static final String INPUT_JSON_SCHEMA = """
        {
          "type": "object",
          "properties": {
            "exampleParam": {
              "type": "string",
              "description": "Example parameter"
            }
          },
          "required": ["exampleParam"]
        }
        """;


    @Override
    public void define( AiToolBuilder builder )
    {
        builder
            .id( "example-tool" )
            .name( "example-tool-name" )
            .userDescription( "example-tool-description-shown-to-user" )
            .aiDescription( "example-tool-description-shown-to-ai" )
            .inputJsonSchema( INPUT_JSON_SCHEMA )
            .scope( AiToolScope.SERVER );
    }

    @Override
    @Transactional( readOnly = true )
    public String execute( String input )
    {
        try
        {
			String exampleParam = extractExampleParam( input );
            if ( StringUtils.isBlank( exampleParam ) )
            {
                return "Parameter exampleParam is required.";
            }
            return OBJECT_MAPPER.writeValueAsString( exampleParam + " answered" );
        }
        catch ( Exception e )
        {
            return "Invalid input data";
        }
    }

    private String extractExampleParam( String input )
    {
        if ( StringUtils.isBlank( input ) )
        {
            return null;
        }
        try
        {
            JsonNode node = OBJECT_MAPPER.readTree( input );
            if ( node.isTextual() )
            {
                return node.asText();
            }
            return node.path( "exampleParam" ).asText( null );
        }
        catch ( Exception e )
        {
            throw new RuntimeException( "Could not extract parameter", e );
        }
    }
}

Parametry:

  • userDescription (wymagany)
    • opis narzędzia wyświetlany użytkownikowi
  • aiDescription (wymagany)
    • opis narzędzia przekazywany do modelów LLM
  • scope (wymagany)
    • enum określający możliwe strony wywołania narzędzia
    • możliwe wartości
      • SERVER - tylko po stronie serwera
      • CLIENT - tylko po stronie przeglądarki
      • ALL - możliwość wykonania w obu miejscach
  • inputJsonSchema (opcjonalny)
    • string ze schematem JSON dla modelów LLM opisujący parametry wejściowe narzędzia przekazane do parametru String input

Implementacja metody execute

...