🤖
OpenAI Integration
Popular
AI & Machine Learning
Use SERPII data with GPT function calling for real-time search augmented generation (RAG)
Official DocumentationFeatures
Function Calling
Use SERPII as a tool in GPT function calling for real-time web search capabilities.
RAG Pipelines
Build retrieval augmented generation systems with fresh SERP data.
Real-time Data
Provide GPT with up-to-date information from search engines.
Context Enhancement
Enhance AI responses with relevant search results and knowledge.
Setup
Step 1
Install Dependencies
Install the OpenAI SDK and axios for API calls.
npm install openai axiosStep 2
Configure API Keys
Set up your OpenAI and SERPII API keys in environment variables.
# .env
OPENAI_API_KEY=sk-...
SERPII_API_KEY=your_serpii_api_keyStep 3
Create SERPII Function
Define a function that GPT can call to search the web.
const searchTool = {
type: "function",
function: {
name: "search_web",
description: "Search the web for current information",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "The search query"
},
engine: {
type: "string",
enum: ["google", "bing", "yahoo"],
description: "Search engine to use"
}
},
required: ["query"]
}
}
};Code Examples
import OpenAI from "openai";
import axios from "axios";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
async function searchWeb(query, engine = "google") {
const response = await axios.get(
`https://api.serpii.com/v1/${engine}/light/search`,
{
params: { q: query },
headers: {
"x-api-key": process.env.SERPII_API_KEY
}
}
);
return response.data.organic_results;
}
async function chat(userMessage) {
const messages = [
{
role: "user",
content: userMessage
}
];
// First API call - GPT decides if it needs to search
let response = await openai.chat.completions.create({
model: "gpt-4-turbo-preview",
messages,
tools: [searchTool],
tool_choice: "auto"
});
// If GPT wants to search
if (response.choices[0].message.tool_calls) {
const toolCall = response.choices[0].message.tool_calls[0];
const args = JSON.parse(toolCall.function.arguments);
// Call SERPII
const searchResults = await searchWeb(args.query, args.engine);
// Add search results to conversation
messages.push(response.choices[0].message);
messages.push({
role: "tool",
tool_call_id: toolCall.id,
content: JSON.stringify(searchResults.slice(0, 5))
});
// Get final response with search context
response = await openai.chat.completions.create({
model: "gpt-4-turbo-preview",
messages
});
}
return response.choices[0].message.content;
}
// Example usage
const answer = await chat("What are the latest trends in AI for 2024?");
console.log(answer);Common Use Cases
- →Real-time web search for AI chatbots
- →Fact-checking and verification
- →Research assistance with current data
- →Content generation with up-to-date information
- →Competitive intelligence gathering
- →News aggregation and summarization
Need help with this integration? Check our API documentation or contact support.