OpenAI Function Calling: Integrate with SAP BTP Destination for External API call
2023-12-2 15:58:51 Author: blogs.sap.com(查看原文) 阅读量:12 收藏

What is Function Calling?

Function calling is a new feature in OpenAI’s GPT-4-0613 and GPT-3.5 Turbo-0613 models. These AI models are trained to detect the need for function calling based on the user’s prompt and respond with a structured call request instead of regular text.

Function calling allows chatbots to interact with other systems, enabling the GPT models to respond to questions they otherwise could not, such as those requiring real-time information or data not included in their training set. In other words, function calling provides another way to teach AI models how to interact with the external world.

What is SAP BTP Destination?

SAP BTP Destination is a cloud-based service that facilitates secure and reliable access to external APIs. It acts as a gateway between SAP Business Technology Platform (BTP) applications and external systems. By utilizing SAP BTP Destination, businesses can integrate various third-party services into their BTP landscape, fostering a connected and data-driven ecosystem.

Integrating OpenAI Function Calling with SAP BTP Destination

Integrating OpenAI Function Calling with SAP BTP Destination empowers developers to execute external API calls directly from within their OpenAI functions. This integration eliminates the need for manual API calls and streamlines the process of accessing external data and services.


What is the purpose of function calling?

Before function calling, there were only two ways of augmenting the capabilities of a GPT language model:

  • Fine-tuning: further training the language model by providing example responses. Fine-tuning is a powerful technique, but it requires significant work (and cost) to prepare the training data. In addition, only a few older models can be fine-tuned until OpenAI enables this feature in GPT-3.5 and GPT-4 models.
  • Embeddings: enriching the prompt with context data can extend the bot’s knowledge and create more accurate responses. The downside is that this context can take up a lot of tokens, increasing the cost and leaving fewer tokens free for building complex responses.

Function calling adds a third way of extending the GPT capabilities by allowing it to ask us to run functions on its behalf. The model can then take the function’s result and build a human-readable response that fits seamlessly into the current conversation.

How to Use Function Calling

The introduction of function calling changes how we interact with the GPT API. Before these functions, the interaction was simple:

  1. Send a prompt to the API.
  2. Receive a response.
  3. Repeat.

Image source : https://semaphoreci.com/blog/function-calling

With function calling, the sequence becomes more involved:

  1. Send the user prompt along with a list of callable functions.
  2. The GPT model responds with either a regular text response or a function call request.
  3. If the model requests a function call, your chatbot’s job is to execute it and return the results to the API.
  4. Using the supplied data, the model then forms a coherent text response. However, in some cases, the API may request a new function call.

Image source : https://semaphoreci.com/blog/function-calling


Defining a Function to Call using SAP BTP Destination for an Interna/External APIs Call

Prerequisites

Before embarking on this integration journey, ensure you have the following prerequisites in place:

  • An OpenAI account with access to the desired AI models
  • Pick any external API or Internal SAP API endpoint to pull results.
  • An SAP BTP account with access to SAP BTP Destination
  • Basic understanding of OpenAI Function Calling and SAP BTP Destination

High-Level Flow of Execution

Create new destination with Internal SAP API or external API

Using the SAP BTP Destination API endpoint, you can make a call to get the results.

def get_top_credits(query: str = None, payee: str = None):
    """Retrieve top 10 payments (API key required)"""

    base_url = destination["destinationConfiguration"]["URL"]
    headers= {'Accept': 'application/json', 'Authorization': token["type"] + ' ' + token["value"],'cache-control': 'no-cache' }

    if query is not None:
        params['q'] = query
    if payee is not None:
        params['payee'] = payeeId


    response = requests.get(base_url, params=params, headers=headers)
    data = response.json()

    if data['status'] == 'ok':
        print(f"Processing {data['totalResults']}")
        return json.dumps(data['payments'])
    else:
        print("Request failed with message:", data['message'])
        return 'No payments found'
   

Integration Steps

  1. Create an SAP BTP Destination:

    • Log in to your SAP BTP account and navigate to the SAP BTP Destination service.
    • Click on “Create Destination” and provide a meaningful name for your destination.
    • Enter the URL of the external API you want to access.
    • Configure authentication settings if the API requires authentication.
    • Save the destination configuration.
  2. Obtain the Destination ID:

    • Locate the newly created destination in the SAP BTP Destination service.
    • Copy the destination ID for use in your OpenAI function.
  3. Implement OpenAI Function:

    • Create an OpenAI function using your preferred programming language.
    • Import the necessary libraries for handling external API calls.
    • Use the destination ID retrieved from SAP BTP Destination to establish a connection to the external API.
    • Implement the logic for making external API calls within your OpenAI function.
    • Test the OpenAI function to ensure successful API calls.

References

https://platform.openai.com/docs/guides/function-calling

https://cookbook.openai.com/examples/how_to_call_functions_with_chat_models

API Documentation for SAP BTP Destination : Link


Benefits of Integration

  • Streamlined API Access: Eliminate manual API calls and simplify external data retrieval.
  • Enhanced Security: Leverage SAP BTP Destination’s security features to protect sensitive data.
  • Centralized Management: Manage external API connections centrally within SAP BTP Destination.

Conclusion

Integrating OpenAI Function Calling with SAP BTP Destination empowers developers to seamlessly execute external API calls from within their OpenAI functions. This integration enhances application capabilities, simplifies data access, and promotes a secure and centralized approach to external API management. By embracing these technologies, businesses can unlock new possibilities and drive innovation in today’s dynamic digital landscape.

Additional Considerations

  • API Documentation: Thoroughly review the documentation of the external API you intend to access.
  • Error Handling: Implement robust error handling mechanisms to gracefully handle potential API call failures.
  • Performance Optimization: Optimize API calls for efficiency and minimize response times.

Remember, continuous learning and exploration are key to unlocking the full potential of OpenAI and SAP BTP Destination. As you embark on this integration journey, stay curious, experiment with new approaches, and continuously refine your integration strategy.


Final Thoughts

Function calling is a powerful feature in OpenAI’s GPT models, enabling them to interact with external tools and APIs in a more deterministic and structured manner. This feature lays the groundwork for more dynamic and responsive AI applications capable of providing current information and executing tasks beyond what was previously possible.


文章来源: https://blogs.sap.com/2023/12/02/openai-function-calling-integrate-with-sap-btp-destination-for-external-api-call/
如有侵权请联系:admin#unsafe.sh