Practical API Patterns for Generative AI Applications
Generative AI is transforming software development, but integrating it efficiently into applications requires well-structured API patterns. This article explores essential API patterns for building AI-driven features, inspired by industry best practices.
1. Request-Response Pattern
A fundamental approach where the client sends a request, and the server returns a response synchronously. This is useful when AI inference is fast and users expect real-time results.
Example (OpenAI GPT-4 API Call):
import openai
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": "Explain AI API patterns."}]
)
print(response["choices"][0]["message"]["content"])
When to Use:
- Real-time chatbot interactions.
- Code generation requests.
- Text summarization.
2. Embedding Pattern
Embedding converts text or other data into vector representations that can be used for similarity search, retrieval, and ranking.
Example (OpenAI Embeddings API):
import openai
response = openai.Embedding.create(
model="text-embedding-ada-002",
input=["AI API patterns"]
)
print(response["data"][0]["embedding"])
When to Use:
- Semantic search.
- Document retrieval.
- Personalized recommendations.
3. Reranking Pattern
Reranking improves search results by using an AI model to reorder items based on relevance, context, or additional criteria.
Example (Cohere Rerank API):
import cohere
co = cohere.Client("YOUR_API_KEY")
response = co.rerank(
model="rerank-english-v2.0",
query="Best API patterns for AI",
documents=["Request-response", "Streaming", "Batch processing"]
)
print(response["results"][0])
When to Use:
- Improving search engine results.
- Prioritizing most relevant AI responses.
- Personalized content ranking.
4. Query Rewriting Pattern
Query rewriting enhances user queries by expanding, refining, or restructuring them before processing.
Example (AI-powered Query Expansion):
import openai
query = "Best AI API patterns"
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": f"Rewrite this query to be more detailed: '{query}'"}]
)
print(response["choices"][0]["message"]["content"])
When to Use:
- Improving search accuracy.
- Enhancing chatbot understanding.
- Refining vague or ambiguous user queries.
5. Retrieval-Augmented Generation (RAG) Pattern
Combining generative AI with an external knowledge base enhances the accuracy and relevance of responses. The model retrieves context before generating a response.
Example (LangChain with OpenAI and Vector Store):
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
from langchain.vectorstores import FAISS
retriever = FAISS.load_local("my_vector_store").as_retriever()
qa_chain = RetrievalQA.from_chain_type(llm=OpenAI(), retriever=retriever)
print(qa_chain.run("What is Retrieval-Augmented Generation?"))
When to Use:
- AI chatbots with domain-specific knowledge.
- Document search with contextual AI-generated answers.
- Legal, medical, and technical AI assistants.
6. Function Calling Pattern
AI models can call external functions to enhance capabilities, such as querying a database or fetching real-time data.
Example (OpenAI Function Calling API):
import openai
def get_weather(location):
return {"location": location, "temperature": "22°C"}
functions = [{
"name": "get_weather",
"parameters": {"type": "object", "properties": {"location": {"type": "string"}}},
}]
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": "What's the weather in Dublin?"}],
functions=functions
)
print(get_weather(response["function_calls"][0]["arguments"]["location"]))
When to Use:
- AI-powered automation workflows.
- Data-driven conversational agents.
- AI-enhanced APIs that interact with real-world data.
Conclusion
Selecting the right API pattern depends on your AI application’s needs—whether it’s embedding for similarity search, reranking for improving search relevance, or query rewriting for refining input queries. By integrating these patterns effectively, developers can build robust, scalable AI applications.
Do you have a favorite AI API pattern? Share your thoughts in the comments!
Hope this helps!
Happy Coding….