|
|
|
import { describe, it, expect } from "vitest"; |
|
import { modifySnippet } from "./snippets.js"; |
|
|
|
|
|
const SHARED_OBJ_TO_ADD = { |
|
maxTokens: 200, |
|
frequencyPenalty: 0.3, |
|
presencePenalty: null, |
|
stopSequences: ["stop1", "stop2"], |
|
complexObject: { |
|
nestedKey: "nestedValue", |
|
nestedNum: 123, |
|
nestedBool: false, |
|
nestedArr: ["a", 1, true, null], |
|
}, |
|
anotherString: "test string", |
|
isStreaming: true, |
|
}; |
|
|
|
|
|
|
|
function createValueRegex(value: unknown, language: "js" | "python" | "json"): string { |
|
|
|
const ws = "(?:\\s|\\n)*"; |
|
|
|
if (typeof value === "string") { |
|
return `"${value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}"`; |
|
} |
|
if (value === null) { |
|
return language === "python" ? "None" : "null"; |
|
} |
|
if (typeof value === "boolean") { |
|
return language === "python" ? (value ? "True" : "False") : String(value); |
|
} |
|
if (typeof value === "number") { |
|
return String(value); |
|
} |
|
if (Array.isArray(value)) { |
|
if (value.length === 0) { |
|
return `\\[${ws}\\]`; |
|
} |
|
const itemRegexes = value.map(item => createValueRegex(item, language)).join(`${ws},${ws}`); |
|
return `\\[${ws}${itemRegexes}${ws}\\]`; |
|
} |
|
if (typeof value === "object" && value !== null) { |
|
const entries = Object.entries(value); |
|
if (entries.length === 0) { |
|
return `\\{${ws}\\}`; |
|
} |
|
const entriesRegex = entries |
|
.map(([k, v]) => { |
|
|
|
|
|
const keyRegex = `"${k.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}"`; |
|
const valueRegexPart = createValueRegex(v, language); |
|
return `${keyRegex}${ws}:${ws}${valueRegexPart}`; |
|
}) |
|
.join(`${ws},${ws}`); |
|
return `\\{${ws}${entriesRegex}${ws}\\}`; |
|
} |
|
return String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
|
} |
|
|
|
type TestCase = { |
|
snippet: string; |
|
objToAdd: Record<string, unknown>; |
|
description: string; |
|
pythonSyntax?: "kwargs" | "dict"; |
|
}; |
|
|
|
|
|
const jsTestCases: TestCase[] = [ |
|
{ |
|
description: "JavaScript InferenceClient with chatCompletionStream", |
|
snippet: ` |
|
import { InferenceClient } from "@huggingface/inference"; |
|
|
|
const client = new InferenceClient("YOUR_HF_TOKEN"); |
|
|
|
let out = ""; |
|
|
|
const stream = client.chatCompletionStream({ |
|
provider: "cerebras", |
|
model: "Qwen/Qwen3-32B", |
|
messages: [ |
|
{ |
|
role: "user", |
|
content: "What is the capital of Brazil?", |
|
}, |
|
{ |
|
content: "{\"answer_the_question\": 5, \"how_many_legs_does_a_dog_have\": true, \"answer_the_question_HERE\": \"elderberry\"}", |
|
role: "assistant", |
|
}, |
|
], |
|
temperature: 0.5, |
|
top_p: 0.7, |
|
}); |
|
|
|
for await (const chunk of stream) { |
|
if (chunk.choices && chunk.choices.length > 0) { |
|
const newContent = chunk.choices[0].delta.content; |
|
out += newContent; |
|
console.log(newContent); |
|
} |
|
} |
|
`, |
|
objToAdd: SHARED_OBJ_TO_ADD, |
|
}, |
|
{ |
|
description: "JavaScript InferenceClient without streaming", |
|
snippet: `import { InferenceClient } from "@huggingface/inference"; |
|
|
|
const client = new InferenceClient("YOUR_HF_TOKEN"); |
|
|
|
const chatCompletion = await client.chatCompletion({ |
|
provider: "cohere", |
|
model: "CohereLabs/c4ai-command-r-plus", |
|
messages: [ |
|
{ |
|
role: "user", |
|
content: "hey, how are you???", |
|
}, |
|
{ |
|
role: "assistant", |
|
content: "{ \"answer_the_question\": \n\n\n\n \n \n\n-1.175\n\n\n\n, \n \"how_many_legs_does_a_dog_have\": \nfalse, \n\n\"answer_the_question_HERE\": \n\n\"A\"\n\n\n}", |
|
}, |
|
], |
|
temperature: 0.5, |
|
top_p: 0.7, |
|
}); |
|
|
|
console.log(chatCompletion.choices[0].message);`, |
|
objToAdd: SHARED_OBJ_TO_ADD, |
|
}, |
|
{ |
|
description: "JavaScript with OpenAI library", |
|
snippet: `import { OpenAI } from "openai"; |
|
|
|
const client = new OpenAI({ |
|
baseURL: "https://api.cerebras.ai/v1", |
|
apiKey: "YOUR_HF_TOKEN", |
|
}); |
|
|
|
const stream = await client.chat.completions.create({ |
|
model: "qwen-3-32b", |
|
messages: [ |
|
{ |
|
role: "user", |
|
content: "What is the capital of Brazil?", |
|
}, |
|
{ |
|
content: "{\"answer_the_question\": 5, \"how_many_legs_does_a_dog_have\": true, \"answer_the_question_HERE\": \"elderberry\"}", |
|
role: "assistant", |
|
}, |
|
], |
|
temperature: 0.5, |
|
top_p: 0.7, |
|
stream: true, |
|
}); |
|
|
|
for await (const chunk of stream) { |
|
process.stdout.write(chunk.choices[0]?.delta?.content || ""); |
|
}`, |
|
objToAdd: SHARED_OBJ_TO_ADD, |
|
}, |
|
]; |
|
|
|
|
|
const pythonTestCases: TestCase[] = [ |
|
{ |
|
description: "Python HuggingFace client", |
|
snippet: `from huggingface_hub import InferenceClient |
|
|
|
client = InferenceClient( |
|
provider="cerebras", |
|
api_key="YOUR_HF_TOKEN", |
|
) |
|
|
|
stream = client.chat.completions.create( |
|
model="Qwen/Qwen3-32B", |
|
messages=[ |
|
{ |
|
"role": "user", |
|
"content": "What is the capital of Brazil?" |
|
}, |
|
{ |
|
"content": "{\"answer_the_question\": 5, \"how_many_legs_does_a_dog_have\": true, \"answer_the_question_HERE\": \"elderberry\"}", |
|
"role": "assistant" |
|
} |
|
], |
|
temperature=0.5, |
|
top_p=0.7, |
|
stream=True, |
|
) |
|
|
|
for chunk in stream: |
|
print(chunk.choices[0].delta.content, end="")`, |
|
objToAdd: SHARED_OBJ_TO_ADD, |
|
pythonSyntax: "kwargs", |
|
}, |
|
{ |
|
description: "Python with Requests", |
|
snippet: `import json |
|
import requests |
|
|
|
API_URL = "https://api.cerebras.ai/v1/chat/completions" |
|
headers = { |
|
"Authorization": "Bearer YOUR_HF_TOKEN", |
|
} |
|
|
|
def query(payload): |
|
response = requests.post(API_URL, headers=headers, json=payload, stream=True) |
|
for line in response.iter_lines(): |
|
if not line.startswith(b"data:"): |
|
continue |
|
if line.strip() == b"data: [DONE]": |
|
return |
|
yield json.loads(line.decode("utf-8").lstrip("data:").rstrip("/n")) |
|
|
|
chunks = query({ |
|
"messages": [ |
|
{ |
|
"role": "user", |
|
"content": "What is the capital of Brazil?" |
|
}, |
|
{ |
|
"content": "{\"answer_the_question\": 5, \"how_many_legs_does_a_dog_have\": true, \"answer_the_question_HERE\": \"elderberry\"}", |
|
"role": "assistant" |
|
} |
|
], |
|
"temperature": 0.5, |
|
"top_p": 0.7, |
|
"model": "qwen-3-32b", |
|
"stream": True, |
|
}) |
|
|
|
for chunk in chunks: |
|
print(chunk["choices"][0]["delta"]["content"], end="")`, |
|
objToAdd: SHARED_OBJ_TO_ADD, |
|
pythonSyntax: "dict", |
|
}, |
|
{ |
|
description: "Python OpenAI client", |
|
snippet: `from openai import OpenAI |
|
|
|
client = OpenAI( |
|
base_url="https://api.cerebras.ai/v1", |
|
api_key="YOUR_HF_TOKEN", |
|
) |
|
|
|
stream = client.chat.completions.create( |
|
model="qwen-3-32b", |
|
messages=[ |
|
{ |
|
"role": "user", |
|
"content": "What is the capital of Brazil?" |
|
}, |
|
{ |
|
"content": "{\"answer_the_question\": 5, \"how_many_legs_does_a_dog_have\": true, \"answer_the_question_HERE\": \"elderberry\"}", |
|
"role": "assistant" |
|
} |
|
], |
|
temperature=0.5, |
|
top_p=0.7, |
|
stream=True, |
|
) |
|
|
|
for chunk in stream: |
|
print(chunk.choices[0].delta.content, end="")`, |
|
objToAdd: SHARED_OBJ_TO_ADD, |
|
pythonSyntax: "kwargs", |
|
}, |
|
|
|
]; |
|
|
|
|
|
const shellTestCases: TestCase[] = [ |
|
{ |
|
description: "curl request to Cerebras API", |
|
snippet: ` |
|
curl https://api.cerebras.ai/v1/chat/completions \\ |
|
-H 'Authorization: Bearer YOUR_HF_TOKEN' \\ |
|
-H 'Content-Type: application/json' \\ |
|
-d '{ |
|
"messages": [ |
|
{ |
|
"role": "user", |
|
"content": "What is the capital of Brazil?" |
|
}, |
|
{ |
|
"content": "{\"answer_the_question\": 5, \"how_many_legs_does_a_dog_have\": true, \"answer_the_question_HERE\": \"elderberry\"}", |
|
"role": "assistant" |
|
} |
|
], |
|
"temperature": 0.5, |
|
"top_p": 0.7, |
|
"model": "qwen-3-32b", |
|
"stream": false |
|
}' |
|
`, |
|
objToAdd: SHARED_OBJ_TO_ADD, |
|
}, |
|
|
|
]; |
|
|
|
describe("modifySnippet", () => { |
|
|
|
describe("JavaScript/TypeScript", () => { |
|
jsTestCases.forEach((testCase, index) => { |
|
it(`should add properties to JS snippet #${index + 1}: ${testCase.description}`, () => { |
|
const result = modifySnippet(testCase.snippet, testCase.objToAdd); |
|
|
|
|
|
Object.entries(testCase.objToAdd).forEach(([key, value]) => { |
|
|
|
|
|
const valueRegexStr = createValueRegex(value, "js"); |
|
|
|
|
|
const propertyPattern = new RegExp(`"${key}"\\s*:\\s*${valueRegexStr}|${key}\\s*:\\s*${valueRegexStr}`); |
|
expect(result).toMatch(propertyPattern); |
|
}); |
|
|
|
|
|
expect(result).toContain("temperature: 0.5"); |
|
expect(result).toContain("top_p: 0.7"); |
|
|
|
|
|
expect(result.includes("{") && result.includes("}")).toBeTruthy(); |
|
}); |
|
}); |
|
}); |
|
|
|
|
|
describe("Python", () => { |
|
pythonTestCases.forEach((testCase, index) => { |
|
it(`should add properties to Python snippet #${index + 1}: ${testCase.description}`, () => { |
|
const result = modifySnippet(testCase.snippet, testCase.objToAdd); |
|
|
|
|
|
Object.entries(testCase.objToAdd).forEach(([key, value]) => { |
|
const valueRegexStr = createValueRegex(value, "python"); |
|
let propertyPattern; |
|
|
|
if (testCase.pythonSyntax === "dict") { |
|
|
|
propertyPattern = new RegExp(`"${key}"\\s*:\\s*${valueRegexStr}`); |
|
} else { |
|
|
|
|
|
const snakeKey = key.replace(/([A-Z])/g, "_$1").toLowerCase(); |
|
propertyPattern = new RegExp(`${snakeKey}\\s*=\\s*${valueRegexStr}`); |
|
} |
|
expect(result).toMatch(propertyPattern); |
|
}); |
|
|
|
|
|
if (testCase.pythonSyntax === "dict") { |
|
expect(result).toContain('"temperature": 0.5'); |
|
expect(result).toContain('"top_p": 0.7'); |
|
expect(result.includes("{") && result.includes("}")).toBeTruthy(); |
|
} else { |
|
|
|
expect(result).toContain("temperature=0.5"); |
|
expect(result).toContain("top_p=0.7"); |
|
expect(result.includes("(") && result.includes(")")).toBeTruthy(); |
|
} |
|
}); |
|
}); |
|
}); |
|
|
|
|
|
describe("Shell/curl", () => { |
|
shellTestCases.forEach((testCase, index) => { |
|
it(`should add properties to shell snippet #${index + 1}: ${testCase.description}`, () => { |
|
const result = modifySnippet(testCase.snippet, testCase.objToAdd); |
|
|
|
|
|
Object.entries(testCase.objToAdd).forEach(([key, value]) => { |
|
|
|
|
|
|
|
const snakeKey = key.replace(/([A-Z])/g, "_$1").toLowerCase(); |
|
const valueRegexStr = createValueRegex(value, "json"); |
|
const propertyPattern = new RegExp(`"${snakeKey}"\\s*:\\s*${valueRegexStr}`); |
|
expect(result).toMatch(propertyPattern); |
|
}); |
|
|
|
|
|
expect(result).toContain(`"temperature": 0.5`); |
|
expect(result).toContain(`"top_p": 0.7`); |
|
|
|
|
|
expect(result.includes("{") && result.includes("}")).toBeTruthy(); |
|
}); |
|
}); |
|
}); |
|
}); |
|
|