1 line
24 KiB
JSON
1 line
24 KiB
JSON
{"repo": "abinz-aiml/llmkit", "files": {"examples/stream.js": {"2572de220b108795ac4b2f8058c88703fa8c224c": {"t": 1785745530.4402294, "n": 6431, "h": "4557ceae2e21f620", "c": "const fs = require(\"fs\");\nconst path = require(\"path\");\nconst readline = require(\"readline\");\nconst yaml = require(\"js-yaml\");\nrequire(\"dotenv\").config({ path: path.join(__dirname, \"..\", \".env\") });\n\nconst config = yaml.load(fs.readFileSync(path.join(__dirname, \"..\", \"llm.yaml\"), \"utf8\"));\n\nconst API_ENDPOINTS = {\n openai: \"https://api.openai.com/v1/chat/completions\",\n anthropic: \"https://api.anthropic.com/v1/messages\",\n groq: \"https://api.groq.com/openai/v1/chat/completions\",\n deepseek: \"https://api.deepseek.com/v1/chat/completions\",\n mistral: \"https://api.mistral.ai/v1/chat/completions\",\n together: \"https://api.together.xyz/v1/chat/completions\",\n};\n\nconst API_KEYS = {\n openai: process.env.OPENAI_API_KEY,\n anthropic: process.env.ANTHROPIC_API_KEY,\n groq: process.env.GROQ_API_KEY,\n deepseek: process.env.DEEPSEEK_API_KEY,\n mistral: process.env.MISTRAL_API_KEY,\n together: process.env.TOGETHER_API_KEY,\n};\n\nif (config.provider !== \"local\" && !API_ENDPOINTS[config.provider]) {\n console.error(`Unknown provider: \"${config.provider}\". Supported: local, openai, anthropic, groq, deepseek, mistral, together`);\n process.exit(1);\n}\n\nif (config.provider !== \"local\" && !API_KEYS[config.provider]) {\n console.error(`Missing API key for provider \"${config.provider}\". Add it to .env.`);\n process.exit(1);\n}\n\nfunction flushSSEBuffer(buffer, isAnthropic) {\n if (!buffer.startsWith(\"data: \") || buffer === \"data: [DONE]\") return;\n try {\n const data = JSON.parse(buffer.slice(6));\n if (isAnthropic) {\n if (data.type === \"error\") { process.stderr.write(`\\nAPI error: ${JSON.stringify(data.error)}\\n`); return; }\n if (data.type === \"content_block_delta\" && data.delta?.text) process.stdout.write(data.delta.text);\n } else {\n if (data.error) { process.stderr.write(`\\nAPI error: ${JSON.stringify(data.error)}\\n`); return; }\n const content = data.choices?.[0]?.delta?.content;\n if (content) process.stdout.write(content);\n }\n } catch {}\n}\n\nconst rl = readline.createInterface({ input: process.stdin, output: process.stdout });\nconsole.log(`llmkit | ${config.provider} / ${config.model} | streaming`);\n\nrl.question(\"You: \", async (prompt) => {\n if (!prompt.trim()) { rl.close(); return; }\n process.stdout.write(\"AI: \");\n try {\n if (config.provider === \"local\") {\n const res = await fetch(\"http://localhost:11434/api/generate\", {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({ model: config.model, prompt, stream: true }),\n });\n if (!res.ok) throw new Error(`Ollama HTTP ${res.status}: ${await res.text()}`);\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n let buffer = \"\";\n let streamError = false;\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n buffer += decoder.decode(value);\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop();\n for (const line of lines) {\n if (!line.trim()) continue;\n try {\n const data = JSON.parse(line);\n if (data.error) { process.stderr.write(`\\nOllama error: ${data.error}\\n`); streamError = true; break; }\n if (data.response) process.stdout.write(data.response);\n } catch {}\n }\n if (streamError) break;\n }\n if (!streamError && buffer.trim()) {\n try {\n const data = JSON.parse(buffer);\n if (data.response) process.stdout.write(data.response);\n } catch {}\n }\n } else if (config.provider === \"anthropic\") {\n const res = await fetch(API_ENDPOINTS.anthropic, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-api-key\": API_KEYS.anthropic,\n \"anthropic-version\": \"2023-06-01\",\n },\n body: JSON.stringify({\n model: config.model,\n max_tokens: 4096,\n stream: true,\n messages: [{ role: \"user\", content: prompt }],\n }),\n });\n if (!res.ok) throw new Error(`Anthropic HTTP ${res.status}: ${await res.text()}`);\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n let buffer = \"\";\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n buffer += decoder.decode(value);\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop();\n for (const line of lines) flushSSEBuffer(line, true);\n }\n if (buffer) flushSSEBuffer(buffer, true);\n } else {\n const res = await fetch(API_ENDPOINTS[config.provider], {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${API_KEYS[config.provider]}`,\n },\n body: JSON.stringify({\n model: config.model,\n messages: [{ role: \"user\", content: prompt }],\n stream: true,\n }),\n });\n if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n let buffer = \"\";\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n buffer += decoder.decode(value);\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop();\n for (const line of lines) flushSSEBuffer(line, false);\n }\n if (buffer) flushSSEBuffer(buffer, false);\n }\n console.log(\"\\n\");\n } catch (err) {\n console.error(`\\nError: ${err.message}`);\n }\n rl.close();\n});\n"}, "e4158a277b3b37f9fb5186ae154bd63f507385bc": {"t": 1785753901.1441736, "n": 6431, "h": "4557ceae2e21f620", "c": "const fs = require(\"fs\");\nconst path = require(\"path\");\nconst readline = require(\"readline\");\nconst yaml = require(\"js-yaml\");\nrequire(\"dotenv\").config({ path: path.join(__dirname, \"..\", \".env\") });\n\nconst config = yaml.load(fs.readFileSync(path.join(__dirname, \"..\", \"llm.yaml\"), \"utf8\"));\n\nconst API_ENDPOINTS = {\n openai: \"https://api.openai.com/v1/chat/completions\",\n anthropic: \"https://api.anthropic.com/v1/messages\",\n groq: \"https://api.groq.com/openai/v1/chat/completions\",\n deepseek: \"https://api.deepseek.com/v1/chat/completions\",\n mistral: \"https://api.mistral.ai/v1/chat/completions\",\n together: \"https://api.together.xyz/v1/chat/completions\",\n};\n\nconst API_KEYS = {\n openai: process.env.OPENAI_API_KEY,\n anthropic: process.env.ANTHROPIC_API_KEY,\n groq: process.env.GROQ_API_KEY,\n deepseek: process.env.DEEPSEEK_API_KEY,\n mistral: process.env.MISTRAL_API_KEY,\n together: process.env.TOGETHER_API_KEY,\n};\n\nif (config.provider !== \"local\" && !API_ENDPOINTS[config.provider]) {\n console.error(`Unknown provider: \"${config.provider}\". Supported: local, openai, anthropic, groq, deepseek, mistral, together`);\n process.exit(1);\n}\n\nif (config.provider !== \"local\" && !API_KEYS[config.provider]) {\n console.error(`Missing API key for provider \"${config.provider}\". Add it to .env.`);\n process.exit(1);\n}\n\nfunction flushSSEBuffer(buffer, isAnthropic) {\n if (!buffer.startsWith(\"data: \") || buffer === \"data: [DONE]\") return;\n try {\n const data = JSON.parse(buffer.slice(6));\n if (isAnthropic) {\n if (data.type === \"error\") { process.stderr.write(`\\nAPI error: ${JSON.stringify(data.error)}\\n`); return; }\n if (data.type === \"content_block_delta\" && data.delta?.text) process.stdout.write(data.delta.text);\n } else {\n if (data.error) { process.stderr.write(`\\nAPI error: ${JSON.stringify(data.error)}\\n`); return; }\n const content = data.choices?.[0]?.delta?.content;\n if (content) process.stdout.write(content);\n }\n } catch {}\n}\n\nconst rl = readline.createInterface({ input: process.stdin, output: process.stdout });\nconsole.log(`llmkit | ${config.provider} / ${config.model} | streaming`);\n\nrl.question(\"You: \", async (prompt) => {\n if (!prompt.trim()) { rl.close(); return; }\n process.stdout.write(\"AI: \");\n try {\n if (config.provider === \"local\") {\n const res = await fetch(\"http://localhost:11434/api/generate\", {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({ model: config.model, prompt, stream: true }),\n });\n if (!res.ok) throw new Error(`Ollama HTTP ${res.status}: ${await res.text()}`);\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n let buffer = \"\";\n let streamError = false;\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n buffer += decoder.decode(value);\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop();\n for (const line of lines) {\n if (!line.trim()) continue;\n try {\n const data = JSON.parse(line);\n if (data.error) { process.stderr.write(`\\nOllama error: ${data.error}\\n`); streamError = true; break; }\n if (data.response) process.stdout.write(data.response);\n } catch {}\n }\n if (streamError) break;\n }\n if (!streamError && buffer.trim()) {\n try {\n const data = JSON.parse(buffer);\n if (data.response) process.stdout.write(data.response);\n } catch {}\n }\n } else if (config.provider === \"anthropic\") {\n const res = await fetch(API_ENDPOINTS.anthropic, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n \"x-api-key\": API_KEYS.anthropic,\n \"anthropic-version\": \"2023-06-01\",\n },\n body: JSON.stringify({\n model: config.model,\n max_tokens: 4096,\n stream: true,\n messages: [{ role: \"user\", content: prompt }],\n }),\n });\n if (!res.ok) throw new Error(`Anthropic HTTP ${res.status}: ${await res.text()}`);\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n let buffer = \"\";\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n buffer += decoder.decode(value);\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop();\n for (const line of lines) flushSSEBuffer(line, true);\n }\n if (buffer) flushSSEBuffer(buffer, true);\n } else {\n const res = await fetch(API_ENDPOINTS[config.provider], {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${API_KEYS[config.provider]}`,\n },\n body: JSON.stringify({\n model: config.model,\n messages: [{ role: \"user\", content: prompt }],\n stream: true,\n }),\n });\n if (!res.ok) throw new Error(`HTTP ${res.status}: ${await res.text()}`);\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n let buffer = \"\";\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n buffer += decoder.decode(value);\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop();\n for (const line of lines) flushSSEBuffer(line, false);\n }\n if (buffer) flushSSEBuffer(buffer, false);\n }\n console.log(\"\\n\");\n } catch (err) {\n console.error(`\\nError: ${err.message}`);\n }\n rl.close();\n});\n"}, "76d9a8799daa10b1821e0b1156e1efbb9aec7a4f": {"t": 1785753902.4660437, "n": 3563, "h": "62eca742da366c8f", "c": "const fs = require(\"fs\");\nconst path = require(\"path\");\nconst readline = require(\"readline\");\nconst yaml = require(\"js-yaml\");\nrequire(\"dotenv\").config({ path: path.join(__dirname, \"..\", \".env\") });\n\nconst config = yaml.load(fs.readFileSync(path.join(__dirname, \"..\", \"llm.yaml\"), \"utf8\"));\n\nconst API_ENDPOINTS = {\n openai: \"https://api.openai.com/v1/chat/completions\",\n groq: \"https://api.groq.com/openai/v1/chat/completions\",\n deepseek: \"https://api.deepseek.com/v1/chat/completions\",\n mistral: \"https://api.mistral.ai/v1/chat/completions\",\n together: \"https://api.together.xyz/v1/chat/completions\",\n};\n\nconst API_KEYS = {\n openai: process.env.OPENAI_API_KEY,\n groq: process.env.GROQ_API_KEY,\n deepseek: process.env.DEEPSEEK_API_KEY,\n mistral: process.env.MISTRAL_API_KEY,\n together: process.env.TOGETHER_API_KEY,\n};\n\nif (config.provider === \"anthropic\") {\n console.error(\"Anthropic is not supported in JS. Use: python examples/stream.py\");\n process.exit(1);\n}\n\nconst rl = readline.createInterface({ input: process.stdin, output: process.stdout });\n\nconsole.log(`llmkit | ${config.provider} / ${config.model} | streaming`);\n\nrl.question(\"You: \", async (prompt) => {\n if (!prompt.trim()) { rl.close(); return; }\n process.stdout.write(\"AI: \");\n try {\n\n if (config.provider === \"local\") {\n const res = await fetch(\"http://localhost:11434/api/generate\", {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({ model: config.model, prompt, stream: true }),\n });\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n let buffer = \"\";\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n buffer += decoder.decode(value);\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop();\n for (const line of lines) {\n if (!line.trim()) continue;\n try {\n const data = JSON.parse(line);\n if (data.response) process.stdout.write(data.response);\n } catch {}\n }\n }\n } else {\n const res = await fetch(API_ENDPOINTS[config.provider], {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${API_KEYS[config.provider]}`,\n },\n body: JSON.stringify({\n model: config.model,\n messages: [{ role: \"user\", content: prompt }],\n stream: true,\n }),\n });\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n let buffer = \"\";\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n buffer += decoder.decode(value);\n const lines = buffer.split(\"\\n\");\n buffer = lines.pop();\n for (const line of lines) {\n if (!line.startsWith(\"data: \") || line === \"data: [DONE]\") continue;\n try {\n const data = JSON.parse(line.slice(6));\n const content = data.choices?.[0]?.delta?.content;\n if (content) process.stdout.write(content);\n } catch {}\n }\n }\n }\n\n console.log(\"\\n\");\n } catch (err) {\n console.error(`\\nError: ${err.message}`);\n }\n rl.close();\n});\n"}, "d878d9e0e4bc9311706e292a09ebd8c7c97f14ee": {"t": 1785753903.6381185, "n": 3200, "h": "494f122673e71083", "c": "const fs = require(\"fs\");\nconst path = require(\"path\");\nconst readline = require(\"readline\");\nconst yaml = require(\"js-yaml\");\nrequire(\"dotenv\").config({ path: path.join(__dirname, \"..\", \".env\") });\n\nconst config = yaml.load(fs.readFileSync(path.join(__dirname, \"..\", \"llm.yaml\"), \"utf8\"));\n\nconst API_ENDPOINTS = {\n openai: \"https://api.openai.com/v1/chat/completions\",\n groq: \"https://api.groq.com/openai/v1/chat/completions\",\n deepseek: \"https://api.deepseek.com/v1/chat/completions\",\n mistral: \"https://api.mistral.ai/v1/chat/completions\",\n together: \"https://api.together.xyz/v1/chat/completions\",\n};\n\nconst API_KEYS = {\n openai: process.env.OPENAI_API_KEY,\n groq: process.env.GROQ_API_KEY,\n deepseek: process.env.DEEPSEEK_API_KEY,\n mistral: process.env.MISTRAL_API_KEY,\n together: process.env.TOGETHER_API_KEY,\n};\n\nif (config.provider === \"anthropic\") {\n console.error(\"Anthropic is not supported in JS. Use: python examples/stream.py\");\n process.exit(1);\n}\n\nconst rl = readline.createInterface({ input: process.stdin, output: process.stdout });\n\nconsole.log(`llmkit | ${config.provider} / ${config.model} | streaming`);\n\nrl.question(\"You: \", async (prompt) => {\n process.stdout.write(\"AI: \");\n try {\n\n if (config.provider === \"local\") {\n const res = await fetch(\"http://localhost:11434/api/generate\", {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({ model: config.model, prompt, stream: true }),\n });\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n const lines = decoder.decode(value).split(\"\\n\").filter(Boolean);\n for (const line of lines) {\n const data = JSON.parse(line);\n if (data.response) process.stdout.write(data.response);\n }\n }\n } else {\n const res = await fetch(API_ENDPOINTS[config.provider], {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${API_KEYS[config.provider]}`,\n },\n body: JSON.stringify({\n model: config.model,\n messages: [{ role: \"user\", content: prompt }],\n stream: true,\n }),\n });\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n const lines = decoder.decode(value)\n .split(\"\\n\")\n .filter(l => l.startsWith(\"data: \") && l !== \"data: [DONE]\");\n for (const line of lines) {\n const data = JSON.parse(line.slice(6));\n const content = data.choices?.[0]?.delta?.content;\n if (content) process.stdout.write(content);\n }\n }\n }\n\n console.log(\"\\n\");\n } catch (err) {\n console.error(`\\nError: ${err.message}`);\n }\n rl.close();\n});\n"}, "b5f4d815abde68e4c0347de868419a6bffcea3a0": {"t": 1785753904.933051, "n": 2862, "h": "a65d33bbb67b36af", "c": "const fs = require(\"fs\");\nconst readline = require(\"readline\");\nconst yaml = require(\"js-yaml\");\nrequire(\"dotenv\").config();\n\nconst config = yaml.load(fs.readFileSync(\"llm.yaml\", \"utf8\"));\n\nconst API_ENDPOINTS = {\n openai: \"https://api.openai.com/v1/chat/completions\",\n groq: \"https://api.groq.com/openai/v1/chat/completions\",\n deepseek: \"https://api.deepseek.com/v1/chat/completions\",\n mistral: \"https://api.mistral.ai/v1/chat/completions\",\n together: \"https://api.together.xyz/v1/chat/completions\",\n};\n\nconst API_KEYS = {\n openai: process.env.OPENAI_API_KEY,\n groq: process.env.GROQ_API_KEY,\n deepseek: process.env.DEEPSEEK_API_KEY,\n mistral: process.env.MISTRAL_API_KEY,\n together: process.env.TOGETHER_API_KEY,\n};\n\nconst rl = readline.createInterface({ input: process.stdin, output: process.stdout });\n\nconsole.log(`llmkit | ${config.provider} / ${config.model} | streaming`);\n\nrl.question(\"You: \", async (prompt) => {\n process.stdout.write(\"AI: \");\n\n if (config.provider === \"local\") {\n const res = await fetch(\"http://localhost:11434/api/generate\", {\n method: \"POST\",\n headers: { \"Content-Type\": \"application/json\" },\n body: JSON.stringify({ model: config.model, prompt, stream: true }),\n });\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n const lines = decoder.decode(value).split(\"\\n\").filter(Boolean);\n for (const line of lines) {\n const data = JSON.parse(line);\n if (data.response) process.stdout.write(data.response);\n }\n }\n } else {\n const res = await fetch(API_ENDPOINTS[config.provider], {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${API_KEYS[config.provider]}`,\n },\n body: JSON.stringify({\n model: config.model,\n messages: [{ role: \"user\", content: prompt }],\n stream: true,\n }),\n });\n const reader = res.body.getReader();\n const decoder = new TextDecoder();\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n const lines = decoder.decode(value)\n .split(\"\\n\")\n .filter(l => l.startsWith(\"data: \") && l !== \"data: [DONE]\");\n for (const line of lines) {\n const data = JSON.parse(line.slice(6));\n const content = data.choices?.[0]?.delta?.content;\n if (content) process.stdout.write(content);\n }\n }\n }\n\n console.log(\"\\n\");\n rl.close();\n});\n"}}}} |