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.
- Java 17+
- Gradle (wrapper included)
- OpenAI API key
- Fixer API key (or compatible endpoint returning
latest
withbase
,symbols
, andrates
)
Configure via environment variables or edit src/main/resources/application.properties
.
Required properties:
OPENAI_API_KEY
– OpenAI key used by Spring AIFXRATE_API_KEY
– Fixer (or compatible) API keyfxrate.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
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
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
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.
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.
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.
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 configuredfxrate
API usingRestClient
with paramsaccess_key
,base
, andsymbols
.CustomToolCallResultConverter
formats the tool result into a concise plain-text message returned to the client.
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.
- 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.