aboutsummaryrefslogtreecommitdiff
path: root/packages/docs-test/tests/integrations/openai-sdk.ts
blob: c651e7dc8f4fb9a4ed26162bd5748123539e085a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import "dotenv/config"
import OpenAI from "openai"
import { withSupermemory } from "@supermemory/tools/openai"

const OPENAI_API_KEY = process.env.OPENAI_API_KEY

async function testOpenAIWrapper() {
  console.log("=== OpenAI SDK Wrapper ===")

  if (!OPENAI_API_KEY) {
    console.log("⚠ OPENAI_API_KEY not set, skipping live tests")
    return false
  }

  const openai = new OpenAI()

  // Basic wrapper
  const client = withSupermemory(openai, "docs-test-openai")
  console.log("✓ withSupermemory basic")

  // With options
  const clientWithOptions = withSupermemory(openai, "docs-test-openai", {
    mode: "full",
    addMemory: "always",
    verbose: true,
  })
  console.log("✓ withSupermemory with options")
}

async function testChatCompletion() {
  console.log("\n=== Chat Completion with Memory ===")

  if (!OPENAI_API_KEY) {
    console.log("⚠ OPENAI_API_KEY not set, skipping")
    return
  }

  const openai = new OpenAI()
  const client = withSupermemory(openai, "docs-test-openai", {
    mode: "full",
    addMemory: "always",
  })

  // Add context
  const addResponse = await client.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      { role: "user", content: "Remember that my favorite color is blue." },
    ],
    max_tokens: 100,
  })
  console.log("✓ Add context:", addResponse.choices[0]?.message.content?.substring(0, 50))

  // Retrieve context
  const retrieveResponse = await client.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      { role: "user", content: "What do you know about my preferences?" },
    ],
    max_tokens: 100,
  })
  console.log("✓ Retrieve context:", retrieveResponse.choices[0]?.message.content?.substring(0, 50))
}

async function main() {
  console.log("OpenAI SDK Integration Tests")
  console.log("============================\n")

  const hasKey = await testOpenAIWrapper()
  if (hasKey !== false) {
    await testChatCompletion()
  }

  console.log("\n============================")
  console.log("✅ All OpenAI SDK tests passed!")
}

main().catch(console.error)