AI Endpoints - Utiliser les appels de fonctions avec LangChain4J
AI Endpoints is covered by the OVHcloud AI Endpoints Conditions and the OVHcloud Public Cloud Special Conditions.
Objective
Stable Diffusion is a powerful artificial intelligence model to generate images from text descriptions. You can use it, thanks to AI Endpoints by simply calling the endpoint with a prompt. However, creating a good prompt for Stable Diffusion can be challenging.
Function Calling enables the AI system to perform more complex and dynamic tasks, and to leverage external knowledge and services to generate more accurate and informative responses. In the context of image generation, function calling can be used to enhance the quality of the prompts by optimizing them thanks to external tool based on a LLM.
In this tutorial, we will show you how to optimize your prompts using Function Calling and OVHcloud AI Endpoints.
To do this, we will use LangChain4j, a Java-based framework inspired by LangChain, designed to simplify the integration of LLMs (Large Language Models) into applications. Note that LangChain4j is not officially maintained by the LangChain team, despite the similar name.
Combined with OVHcloud AI Endpoints which offers both LLM and embedding models, it becomes easy to create advanced, production-ready assistants.

Definition
- Function Calling: Function calling refers to the ability of a language model or AI system AI system to request, invoke, and execute predefined functions or tasks such as data processing, calculations, or external API calls in response to user input or prompts.
- LangChain4j: a Java-based framework inspired by LangChain, designed to simplify the integration of LLMs (Large Language Models) into applications. Note that LangChain4j is not officially maintained by the LangChain team, despite the similar name.
- AI Endpoints: A serverless platform by OVHcloud providing easy access to a variety of world-renowned AI models including Mistral, LLaMA, and more. This platform is designed to be simple, secure, and intuitive, with data privacy as a top priority.
Requirements
- A Public Cloud project in your OVHcloud account.
- An access token for OVHcloud AI Endpoints. To create an API token, follow the instructions in the AI Endpoints - Getting Started guide.
- This code example uses JBang, a Java-based tool for creating and running Java programs as scripts. For more information on JBang, please refer to the JBang documentation.
Instructions
To create our application we will use LangChain4J to simplify the integration of the AI models and the function calling mechanism.
Tool creation
To use the function calling mechanism, we need to define a tool.
In our example the goal of the tool is to call Stable Diffusion API to generate an image.
⚠️ This is not the model itself that calls the tool but the client that invokes the model. ⚠️
⚠️ One of the main point to help the LLM to choose the right tool to use, is to provide clear and comprehensive description. ⚠️
Once the tool is ready, lets tell to the model that it can use it!
Optimizing the model with a tool
First we create a simple chatbot.
It’s not mandatory to create a such detailed system message, but it helps the model to choose the tool when needed.
After this we assemble all the pieces together:
ℹ️ We use a loop to be able to ask the model to optimize the image generation parameters based on the previous response. ℹ️ And that is it!
It’s time to test our Stable Diffusion optimizer.
ℹ️ As you can see, the model translated the prompt 😊
Here is the result of the prompt:

Conclusion
In this article, we have seen how to use Function Calling with OVHcloud AI Endpoints and LangChain4J.
Go further
You can find the full code example in the GitHub repository.
Browse the full AI Endpoints documentation to further understand the main concepts and get started.
To discover how to build complete and powerful applications using AI Endpoints, explore our dedicated AI Endpoints guides.
If you need training or technical assistance to implement our solutions, contact your sales representative or click on this link to get a quote and ask our Professional Services experts for a custom analysis of your project.
Feedback
Please feel free to send us your questions, feedback, and suggestions regarding AI Endpoints and its features:
- In the #ai-endpoints channel of the OVHcloud Discord server, where you can engage with the community and OVHcloud team members.
If you need training or technical assistance to implement our solutions, contact your sales representative or click on this link to get a quote and ask our Professional Services experts for a custom analysis of your project.