OpenAI Adapter
The OpenAI Adapter for GQLPT allows you to leverage OpenAI's powerful language models to generate GraphQL queries from natural language inputs.
Installation
To use the OpenAI Adapter, first install it alongside the core GQLPT library:
npm install gqlpt @gqlpt/adapter-openai
Configuration
To use the OpenAI Adapter, you'll need an API key from OpenAI. You can obtain this from the OpenAI website.
Usage
Here's how to set up and use the OpenAI Adapter with GQLPT:
import { AdapterOpenAI } from "@gqlpt/adapter-openai";
import { GQLPTClient } from "gqlpt";
const client = new GQLPTClient({
typeDefs: `your GraphQL schema here`,
adapter: new AdapterOpenAI({
apiKey: "your-openai-api-key",
// Additional OpenAI-specific options can be added here
}),
});
await client.connect();
const { query, variables } = await client.generateQueryAndVariables(
"Find users named John",
);
console.log(query, variables);
Advanced Configuration
The OpenAI Adapter supports various configuration options to fine-tune its behavior:
model
: Specify the OpenAI model to use (e.g., 'gpt-4', 'gpt-3.5-turbo')temperature
: Control the randomness of the output (0.0 to 1.0)maxTokens
: Set the maximum number of tokens in the generated response
Example with advanced options:
const adapter = new AdapterOpenAI({
apiKey: "your-openai-api-key",
model: "gpt-4",
temperature: 0.7,
maxTokens: 150,
});
Best Practices
- API Key Security: Never expose your OpenAI API key in client-side code. Use environment variables or secure key management systems.
- Error Handling: Implement proper error handling to manage API rate limits and other potential issues.
- Model Selection: Choose the appropriate model based on your needs. GPT-4 is more capable but may be slower and more expensive than GPT-3.5-turbo.
- Cost Management: Monitor your usage to manage costs, especially when using more advanced models.
Troubleshooting
If you encounter issues:
- Verify your API key is correct and has the necessary permissions.
- Check your network connection and firewall settings.
- Ensure you're not exceeding OpenAI's rate limits.
- Review OpenAI's status page for any ongoing service issues.
For more detailed information, refer to the OpenAI API documentation.