Empowering Models: Analyzing Bella OpenAPI's Function Call Mechanism
Introduction: Why Function Call Capabilities Are Needed
Large language models (LLMs) have progressed from simple conversations to complex tool-calling scenarios, with Function Call capabilities becoming crucial for building practical AI applications. However, significant differences exist among models in the market regarding this capability: commercial models like OpenAI, Claude, and Qwen-max natively support Function Call, while many open-source models, including DeepSeek R1, lack this ability. This capability gap creates challenges for enterprises building unified AI platforms. Through in-depth analysis of Bella OpenAPI's code implementation, we reveal how it extends this key capability to models that don't support Function Call through an innovative simulation mechanism.
Limitations of DeepSeek R1 and the Current Industry State
DeepSeek R1, as a powerful open-source large language model, excels in text generation and knowledge understanding, but like many open-source models, it faces a common limitation: it doesn't support native Function Call capabilities. This limitation makes it difficult to apply directly in enterprise-level application scenarios requiring tool calls and structured output.
The current industry situation is:
- Most commercial models support comprehensive Function Call capabilities
- Some open-source models provide limited support
- Many models, including DeepSeek R1, don't support this feature at all
This imbalance forces users to either lock into high-cost commercial models or forego the powerful capabilities that Function Call brings.
Code Analysis: Bella OpenAPI's Simulation Mechanism
By analyzing Bella OpenAPI's source code, we can see its innovative Function Call simulation implementation. The core mechanism is located in the ToolCallSimulator class and SimulationHepler class:
// ToolCallSimulator.java
@Override
public CompletionResponse completion(CompletionRequest request,
String url, T property) {
if(property.isFunctionCallSimulate()) {
CompletionRequest req =
SimulationHepler.rewrite(request);
if(req != null) {
processData.setFunctionCallSimulate(true);
CompletionResponse resp = delegator.completion(req,
url, property);
try {
// Parse as function call, replace the first choice
Choice choice =
SimulationHepler.parse(resp.reasoning(), resp.content());
choice.setFinish_reason(resp.finishReason());
resp.getChoices().set(0, choice);
} catch (Exception e) {
LOGGER.info(resp.content(), e);
}
return resp;
}
}
return delegator.completion(request, url, property);
}
This code demonstrates Bella OpenAPI's adapter pattern, controlling whether to enable simulation functionality through the functionCallSimulate switch. When enabled, the system:
- Rewrites the original request (SimulationHepler.rewrite(request))
- Sends the rewritten request to the LLM
- Parses the model's returned content and converts it to the standard Function Call format
The Secret of Prompt Engineering
The core of the simulation mechanism lies in the function_call_template.pebble template file, which is a carefully designed prompt engineering example:
<Goal>
The goal of this prompt is to introduce function calling capabilities to the model, but in this process, the model's default answering behavior must be preserved, providing detailed, comprehensive, and natural content unless the user explicitly requests a concise answer.
</Goal>
The user is interacting with an agent. Based on the 'agent basic information' and its 'interaction process' with the user, you need to analyze according to 'decision requirements' what action should be taken next from the 'candidate action set', and output the result according to 'output format requirements'.
...
## Candidate Action Set
```python
{% for func in functions %}
{{ func }}
{% endfor %}
def directly_response(type: str, content: str):
"""Directly reply to the user, providing complete, detailed, and natural answers, or collecting necessary parameter information.
...
"""
pass
This template implements Function Call simulation through several key strategies:
- Structured instructions: Clearly telling the model (including DeepSeek R1) how to output in Python function call format
- Role-playing: Having the model play the role of an agent with tool-using capabilities
- Dynamic function definition injection: Dynamically converting function definitions from the original request into Python definitions that the model can understand
- Unified response format: Ensuring consistency through strict output format requirements
Parse Engine: From Text to Structured Calls
After models like DeepSeek R1 generate text responses, the PythonFuncCallParser
and PythonFuncCallListener
classes are responsible for parsing the text into structured Function Calls:
public static CompletionResponse.Choice parse(String reasoning,
String content) throws IOException {
PythonFuncCallListener sfc = new
PythonFuncCallListener(null);
PythonFuncCallParser parser = new PythonFuncCallParser(new
StringReader(content), sfc);
parser.parse();
return sfc.getToolcalls().isEmpty()
? CompletionResponse.assistantMessageChoice(reasoning,
sfc.getBuffer().toString())
: CompletionResponse.toolcallChoice(reasoning,
sfc.getToolcalls());
}
This parsing capability allows Bella OpenAPI to extract function call information from text generated by DeepSeek R1 and convert it to a standard format, thereby achieving the same behavior as models that natively support Function Call.
Practical Effects: Extending DeepSeek R1's Capabilities
Through Bella OpenAPI's Function Call simulation mechanism, DeepSeek R1 gains the following capability extensions:
- Structured output: Generating JSON responses that comply with standards
- Tool calling: Calling external APIs, database queries, and other tools
- Multi-step reasoning: Supporting complex reasoning chains through tool calls
- Standardized interfaces: Maintaining interface consistency with OpenAI models that support Function Call
This extension allows DeepSeek R1 to play a role in a wider range of enterprise application scenarios, especially in scenarios requiring structured output and tool integration.
Technical Details and Performance Considerations
Performance Impact
The Function Call simulation mechanism mainly brings two aspects of performance impact:
- Increased request complexity: Requires generating more complex prompts, leading to increased token count
- Parsing overhead: Requires additional parsing steps to convert text to structured formats
However, compared to native Function Call, the performance difference with the simulation mechanism is acceptable, especially considering the flexibility improvement it brings.
Application Value
Bella OpenAPI's Function Call simulation mechanism provides users with several key values:
- Model selection flexibility: Users can freely choose models based on factors such as cost and performance, without being limited to commercial models that support Function Call
- Cost optimization: Open-source models like DeepSeek R1 can be used in appropriate scenarios to reduce API call costs
- Unified development experience: Development interfaces remain consistent regardless of the underlying model used
- Feature extension: Gives open-source models like DeepSeek R1 stronger application capabilities
This mechanism allows users to fully leverage AI potential while maintaining technical flexibility, building more intelligent and practical applications.
Conclusion: Extending the Capabilities of Open-source AI
Bella OpenAPI's Function Call simulation mechanism demonstrates the innovative value of open-source projects—it not only fills the capability gap of open-source models like DeepSeek R1 but also provides enterprises with greater freedom of technical choice. This design approach is worth emulating by other AI platforms and helps promote the development of the open-source AI ecosystem and the expansion of application scenarios.
Through technological innovation, Bella OpenAPI successfully bridges the AI capability "gap," allowing all types of models to maximize their value and laying the foundation for building more powerful AI applications.
If you want to learn more about Bella OpenAPI's Function Call simulation mechanism or explore its possible applications on models like DeepSeek R1, feel free to visit the GitHub repository or experience the online version directly.