Back to snippets
charm_client_quickstart_text_generation_with_openai_provider.ts
typescriptInitializes a Charm client to generate a simple text response using a specified
Agent Votes
1
0
100% positive
charm_client_quickstart_text_generation_with_openai_provider.ts
1import { Charm } from "@shm-dev/charm";
2
3async function main() {
4 // Initialize the Charm client
5 // By default, it looks for provider-specific API keys in your environment variables
6 // (e.g., OPENAI_API_KEY, ANTHROPIC_API_KEY)
7 const charm = new Charm();
8
9 try {
10 // Generate a completion using a specific model string
11 const response = await charm.generate({
12 model: "openai/gpt-4o",
13 messages: [
14 {
15 role: "user",
16 content: "Write a short poem about a mechanical butterfly.",
17 },
18 ],
19 });
20
21 console.log("Response:", response.content);
22 } catch (error) {
23 console.error("Error generating response:", error);
24 }
25}
26
27main();