Integrate OpenAI APIs with Google Antigravity applications including streaming chat completions and embeddings
# OpenAI API for Google Antigravity Apps
AI capabilities enhance modern applications. This guide establishes patterns for integrating OpenAI APIs with Google Antigravity projects.
## Basic Chat Completion
```typescript
import OpenAI from "openai";
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
export async function generateCompletion(prompt: string): Promise<string> {
const response = await openai.chat.completions.create({
model: "gpt-4-turbo-preview",
messages: [{ role: "user", content: prompt }],
max_tokens: 1000,
});
return response.choices[0]?.message?.content || "";
}
```
## Streaming Response
```typescript
import OpenAI from "openai";
import { OpenAIStream, StreamingTextResponse } from "ai";
export async function POST(req: Request) {
const { messages } = await req.json();
const response = await openai.chat.completions.create({
model: "gpt-4-turbo-preview",
messages,
stream: true,
});
const stream = OpenAIStream(response);
return new StreamingTextResponse(stream);
}
```
## Client-Side Chat
```typescript
"use client";
import { useChat } from "ai/react";
export function ChatInterface() {
const { messages, input, handleInputChange, handleSubmit } = useChat();
return (
<div>
{messages.map((m) => <div key={m.id}>{m.content}</div>)}
<form onSubmit={handleSubmit}>
<input value={input} onChange={handleInputChange} />
<button type="submit">Send</button>
</form>
</div>
);
}
```
## Best Practices
1. **Stream responses**: Better UX for long generations
2. **Error handling**: Handle rate limits
3. **Token management**: Track usage
4. **Caching**: Cache repeated queries
5. **Security**: Never expose API keysThis OpenAI prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their openai implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For OpenAI projects, consider mentioning your framework version, coding style, and any specific libraries you're using.