|
| 1 | +import dotenv from "dotenv"; |
| 2 | +import { OpenAI } from "openai"; |
| 3 | +import { v4 as uuidv4 } from "uuid"; |
| 4 | + |
| 5 | +import { PromptRequest, PromptResponse } from "../../src/api"; |
| 6 | +import { HumanloopClient } from "../../src/humanloop.client"; |
| 7 | + |
| 8 | +export interface TestIdentifiers { |
| 9 | + id: string; |
| 10 | + path: string; |
| 11 | +} |
| 12 | + |
| 13 | +export interface TestPrompt { |
| 14 | + id: string; |
| 15 | + path: string; |
| 16 | + response: PromptResponse; |
| 17 | +} |
| 18 | + |
| 19 | +export interface TestSetup { |
| 20 | + sdkTestDir: TestIdentifiers; |
| 21 | + testPromptConfig: PromptRequest; |
| 22 | + openaiApiKey: string; |
| 23 | + humanloopClient: HumanloopClient; |
| 24 | +} |
| 25 | + |
| 26 | +export function readEnvironment(): void { |
| 27 | + if (![process.env.HUMANLOOP_API_KEY, process.env.OPENAI_API_KEY].every(Boolean)) { |
| 28 | + // Testing locally not in CI, running dotenv.config() would override the secrets set for GitHub Action |
| 29 | + dotenv.config({}); |
| 30 | + } |
| 31 | + if (!process.env.HUMANLOOP_API_KEY) { |
| 32 | + throw new Error("HUMANLOOP_API_KEY is not set"); |
| 33 | + } |
| 34 | + if (!process.env.OPENAI_API_KEY) { |
| 35 | + throw new Error("OPENAI_API_KEY is not set for integration tests"); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export async function setupTestEnvironment(testName: string): Promise<TestSetup> { |
| 40 | + readEnvironment(); |
| 41 | + |
| 42 | + const openaiApiKey = process.env.OPENAI_API_KEY!; |
| 43 | + const humanloopClient = new HumanloopClient({ |
| 44 | + apiKey: process.env.HUMANLOOP_API_KEY, |
| 45 | + instrumentProviders: { |
| 46 | + OpenAI: OpenAI, |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + // Create a test directory |
| 51 | + const directoryPath = `SDK_TEST_${testName}_${uuidv4()}`; |
| 52 | + const response = await humanloopClient.directories.create({ |
| 53 | + path: directoryPath, |
| 54 | + }); |
| 55 | + |
| 56 | + const sdkTestDir = { |
| 57 | + id: response.id, |
| 58 | + path: response.path, |
| 59 | + }; |
| 60 | + |
| 61 | + // Create test prompt config |
| 62 | + const testPromptConfig: PromptRequest = { |
| 63 | + provider: "openai", |
| 64 | + model: "gpt-4o-mini", |
| 65 | + temperature: 0.5, |
| 66 | + template: [ |
| 67 | + { |
| 68 | + role: "system", |
| 69 | + content: "You are a helpful assistant. Answer concisely.", |
| 70 | + }, |
| 71 | + { |
| 72 | + role: "user", |
| 73 | + content: "{{question}}", |
| 74 | + }, |
| 75 | + ], |
| 76 | + }; |
| 77 | + |
| 78 | + return { |
| 79 | + sdkTestDir, |
| 80 | + testPromptConfig, |
| 81 | + openaiApiKey, |
| 82 | + humanloopClient, |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Creates a test prompt in the specified test environment |
| 88 | + */ |
| 89 | +export async function createTestPrompt( |
| 90 | + setup: TestSetup, |
| 91 | + name: string = "test_prompt", |
| 92 | + customConfig?: Partial<PromptRequest>, |
| 93 | +): Promise<TestPrompt> { |
| 94 | + const promptPath = `${setup.sdkTestDir.path}/${name}`; |
| 95 | + const config = customConfig |
| 96 | + ? { ...setup.testPromptConfig, ...customConfig } |
| 97 | + : setup.testPromptConfig; |
| 98 | + |
| 99 | + const promptResponse = await setup.humanloopClient.prompts.upsert({ |
| 100 | + path: promptPath, |
| 101 | + ...config, |
| 102 | + }); |
| 103 | + |
| 104 | + return { |
| 105 | + id: promptResponse.id, |
| 106 | + path: promptPath, |
| 107 | + response: promptResponse, |
| 108 | + }; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Creates a base function for LLM calls that can be decorated |
| 113 | + */ |
| 114 | +export function createBaseLLMFunction(setup: TestSetup, model: string = "gpt-4o-mini") { |
| 115 | + return async (question: string): Promise<string> => { |
| 116 | + const openaiClient = new OpenAI({ apiKey: setup.openaiApiKey }); |
| 117 | + |
| 118 | + const response = await openaiClient.chat.completions.create({ |
| 119 | + model: model, |
| 120 | + messages: [{ role: "user", content: question }], |
| 121 | + }); |
| 122 | + |
| 123 | + return response.choices[0].message.content || ""; |
| 124 | + }; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Applies the prompt decorator to a function and tests it |
| 129 | + */ |
| 130 | +export async function testPromptDecorator( |
| 131 | + setup: TestSetup, |
| 132 | + prompt: TestPrompt, |
| 133 | + input: string = "What is the capital of the France?", |
| 134 | + expectedSubstring: string = "paris", |
| 135 | +): Promise<void> { |
| 136 | + // Create the base function |
| 137 | + const myPromptBase = createBaseLLMFunction(setup); |
| 138 | + |
| 139 | + // Apply the higher-order function instead of decorator |
| 140 | + const myPrompt = setup.humanloopClient.prompt({ |
| 141 | + path: prompt.path, |
| 142 | + callable: myPromptBase, |
| 143 | + }); |
| 144 | + |
| 145 | + // Call the decorated function |
| 146 | + const result = await myPrompt(input); |
| 147 | + if (result) { |
| 148 | + expect(result.toLowerCase()).toContain(expectedSubstring.toLowerCase()); |
| 149 | + } else { |
| 150 | + throw new Error("Expected result to be defined"); |
| 151 | + } |
| 152 | + |
| 153 | + // Wait for 5 seconds for the log to be created |
| 154 | + await new Promise((resolve) => setTimeout(resolve, 5000)); |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Deletes a test prompt |
| 159 | + */ |
| 160 | +export async function deleteTestPrompt( |
| 161 | + setup: TestSetup, |
| 162 | + promptId: string, |
| 163 | +): Promise<void> { |
| 164 | + if (promptId) { |
| 165 | + await setup.humanloopClient.prompts.delete(promptId); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +export async function cleanupTestEnvironment(setup: TestSetup): Promise<void> { |
| 170 | + // Clean up the test directory |
| 171 | + if (setup.sdkTestDir.id) { |
| 172 | + await setup.humanloopClient.directories.delete(setup.sdkTestDir.id); |
| 173 | + } |
| 174 | +} |
0 commit comments