Skip to content

ankodebase/test-openai-tools

Repository files navigation

Spring AI Demo - FX Rate & Image Analysis

A Spring Boot (3.5) demo using Spring AI with OpenAI for both function calling to fetch FX rates from Fixer API and image analysis capabilities. The application demonstrates AI-powered currency conversion and image description/code generation features.

Prerequisites

  • Java 17+
  • Gradle (wrapper included)
  • OpenAI API key
  • Fixer API key (or compatible endpoint returning latest with base, symbols, and rates)

Configuration

Configure via environment variables or edit src/main/resources/application.properties.

Required properties:

  • OPENAI_API_KEY – OpenAI key used by Spring AI
  • FXRATE_API_KEY – Fixer (or compatible) API key
  • fxrate.api-url – Base URL for latest rates (default: https://data.fixer.io/api/latest)

Examples (PowerShell):

$env:OPENAI_API_KEY = "sk-..."
$env:FXRATE_API_KEY = "your_fixer_key"

The project defaults to model gpt-5-nano and temperature 1.0:

  • spring.ai.openai.chat.options.model=gpt-5-nano
  • spring.ai.openai.chat.options.temperature=1.0

Run

Using the Gradle wrapper:

./gradlew bootRun

Or build a jar and run:

./gradlew clean build
java -jar build/libs/test-openai-functions-0.0.1-SNAPSHOT.jar

API

FX Rate Endpoints

GET /fx/rate

Query params:

  • from – source currency code, e.g. USD
  • to – target currency code, e.g. EUR

Example:

GET http://localhost:8080/fx/rate?from=USD&to=EUR

Response: A human-readable string generated via Spring AI function calling, for example:

Current FX rate from USD to EUR is 0.92 at 2025-01-01T12:34:56Z

Image Analysis Endpoints

GET /images/describe

Analyzes a test image (test.jpg) and returns a description of what the AI sees.

Example:

GET http://localhost:8080/images/describe

Response: A detailed description of the image content generated by the AI.

GET /images/code-image/describe

Analyzes a code image (code-image.png) and returns a description of the code or diagram shown.

Example:

GET http://localhost:8080/images/code-image/describe

Response: A description of the code or visual elements in the image.

GET /images/code-image/generate-code

Analyzes a code image (code-image.png) and generates executable code based on what it sees.

Example:

GET http://localhost:8080/images/code-image/generate-code

Response: Generated code based on the visual content of the image.

How it works

FX Rate Functionality

  • FxRateController builds a prompt with system and user messages and enables a tool: FxRateService.
  • FxRateService.getRates is annotated with @Tool so the LLM can call it. It fetches data from the configured fxrate API using RestClient with params access_key, base, and symbols.
  • CustomToolCallResultConverter formats the tool result into a concise plain-text message returned to the client.

Image Analysis Functionality

  • ImageController provides three endpoints for image analysis using Spring AI's vision capabilities.
  • Images are loaded from the classpath (/images/test.jpg and /images/code-image.png) and sent to the OpenAI vision model.
  • The AI analyzes the images and returns descriptions or generates code based on the visual content.
  • Uses Media objects with appropriate MIME types (IMAGE_JPEG, IMAGE_PNG) to send images to the AI model.

Notes

  • Ensure your FX API plan supports the base parameter; free Fixer plans may restrict it.
  • Replace fxrate.api-url or wrap with a proxy if your provider differs.
  • Logging is enabled via @Slf4j for request/response visibility.
  • Image analysis requires OpenAI API access with vision model support (GPT-4 Vision or similar).
  • Sample images are included in src/main/resources/images/ for testing the image endpoints.

About

Call tools for any task by LLM

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages