Simplifying AI Integration with API Standards.
Published: 2024-02-27 14:13:25

The backbone of effective AI integration lies in the establishment and adherence to API standards. These standards are not merely guidelines but are instrumental in ensuring that different components of an application, such as backend services and front-end interfaces, can communicate effortlessly. The project under discussion serves as an exemplary case of this principle in action.

### Example Project Overview

This Flask application acts as a middleman, facilitating communication between an AI model (for example OpenAI ChatGPT) and a user interface built with Streamlit. The key to its swift integration lies in the standardized API endpoints and data exchange formats, which are in line with OpenAI's API standards.

### Standardization at Work

The Flask application defines this endpoint:

**/v1/chat/completions**: Handles requests to generate chat completions based on user prompts.

These endpoints are designed to expect and return data in a structured format, mirroring the standards set by OpenAI. This consistency is crucial for integrating with ChatGPT (or different LLM - includes local deployments) and ensures that adding a new front-end interface like Streamlit is straightforward.

### Code Snippets Demonstrating Standards and Integration

**Flask Endpoint for Chat Completions**:

```python
pythonCopy code
@app.route('/v1/chat/completions', methods=['POST'])
def chat_completions():
    request_data = request.json
    # Standardized request handling
    model = request_data.get('model', 'default-model')
    prompt = request_data.get('prompt', '')
		session_id = request_data.get('session_id')
    security_token = request_data.get('token')
    ...
    response = client.chat.completions.create(model=model, messages=...)
    ...

```

This code snippet shows how the Flask app handles POST requests to **`/v1/chat/completions`**. It adheres to a structured request format, expecting specific fields such as **`model`** and **`prompt`**. This alignment with OpenAI's API standards ensures that the application can easily parse requests and communicate with external AI services. On the top of it, you can extend call with your properties - session_id and security_token.

**Streamlit Interface for User Interaction**:

```python
pythonCopy code
import streamlit as st
import requests

st.title('Chat with AI')
...
response = requests.post(
    api_url,
    json={'model': model, 'prompt': prompt, 'session_id': session_id, 'token': security-token}
)
...

```

Here, the Streamlit script illustrates how the front end consumes the Flask API, sending data in a structured format that matches the expectations of the Flask endpoints. This seamless integration is made possible by the consistent application of API standards across both the Flask application and the Streamlit front end. It's also important to note that you can implement custom properties, as was proposed in the Flask backend app.

### Integrate, Integrate, Integrate

The integration demonstrated in this project underscores the value of API standards in bridging the gap between complex AI functionalities, your services and user-friendly interfaces. Thus, you will be able to integrate open-source products, your custom products, or COTS in a very simple and direct way.

This practical example serves as a blueprint for developers looking to utilizing the capabilities of AI in their applications, highlighting that through the lens of standards, the path to integration is not only viable but streamlined. 

For a deeper dive into the project and its implementation, exploring the [GitHub repository](https://github.com/MKovacik/PoC-OpenAI-API-Standards) will provide additional insights and the full codebase.



| ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjl4pjit0gftn41927dc.png) |
|:--:| 
| *Fig.1 - The diagram presented highlights the difficulties that development teams may face when choosing to implement custom APIs for AI services. Custom solutions often lead to a complex mixture of integrations, each with unique maintenance and compatibility requirements—elements that can slow down the development process and increase the workload. On the other hand, embracing OpenAI API standards can simplify the integration process, promoting consistency and speeding up the progression of development projects. |

Why to use Open AI API standards:
- OpenAI is the most popular format for describing AI APIs, leading to more community support and a proliferation of tools leveraging OpenAI for generating AI enabled applications
- OpenAI API standards provide a clear and concise way to define API endpoints, parameters, and responses, which reduces the risk of errors and bugs during integration, making APIs more developer-friendly and easier to use.
- A valid OpenAPI specification can save significant time and resources, allowing for quick and correct SDK generation, reducing support queries, and simplifying the integration process
- Investing in a fully compliant OpenAPI specification can reduce costs by eliminating the need for managing separate SDK development teams
- OpenAI API standards allow for the creation of AI solutions tailored to specific industries, providing insights that can inform long-term strategy
- Official documentation - [here](https://github.com/openai/openai-openapi)

List of libraries and articles for inspiration:
- https://platform.openai.com/docs/libraries
- https://vercel.com/docs/integrations/ai/openai
- https://github.com/TheoKanning/openai-java
- https://devblogs.microsoft.com/dotnet/getting-started-azure-openai-dotnet/
- https://www.c-sharpcorner.com/article/consume-chat-gpt-open-ai-api-inside-net-core-application-using-razor-pages/
- https://www.linkedin.com/pulse/why-valid-openapi-specification-key-api-success-2023-lessons-melvin/


Thanks [Cyril Sadovsky](https://www.linkedin.com/in/cyril-s-92907566/) to feedback and hints.

** commented: I really like generating picture with AI, so please do not hate me for my header picture.