@voquill/voice-ai 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,26 @@
1
+ import type { JsonResponse, LlmChatInput, LlmStreamEvent } from "@voquill/types";
2
+ export declare const CEREBRAS_MODELS: readonly ["zai-glm-4.7", "llama3.1-8b", "gpt-oss-120b", "qwen-3-235b-a22b-instruct-2507"];
3
+ export type CerebrasModel = (typeof CEREBRAS_MODELS)[number];
4
+ export type CerebrasGenerateTextArgs = {
5
+ apiKey: string;
6
+ model?: CerebrasModel;
7
+ system?: string;
8
+ prompt: string;
9
+ jsonResponse?: JsonResponse;
10
+ };
11
+ export type CerebrasGenerateResponseOutput = {
12
+ text: string;
13
+ tokensUsed: number;
14
+ };
15
+ export declare const cerebrasGenerateTextResponse: ({ apiKey, model, system, prompt, jsonResponse, }: CerebrasGenerateTextArgs) => Promise<CerebrasGenerateResponseOutput>;
16
+ export type CerebrasTestIntegrationArgs = {
17
+ apiKey: string;
18
+ };
19
+ export declare const cerebrasTestIntegration: ({ apiKey, }: CerebrasTestIntegrationArgs) => Promise<boolean>;
20
+ export type CerebrasStreamChatArgs = {
21
+ apiKey: string;
22
+ model: string;
23
+ input: LlmChatInput;
24
+ };
25
+ export declare function cerebrasStreamChat({ apiKey, model, input, }: CerebrasStreamChatArgs): AsyncGenerator<LlmStreamEvent>;
26
+ //# sourceMappingURL=cerebras.utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cerebras.utils.d.ts","sourceRoot":"","sources":["../src/cerebras.utils.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAGjF,eAAO,MAAM,eAAe,2FAKlB,CAAC;AACX,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,CAAC,CAAC;AAkC7D,MAAM,MAAM,wBAAwB,GAAG;IACrC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,aAAa,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,8BAA8B,GAAG;IAC3C,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,eAAO,MAAM,4BAA4B,GAAU,kDAMhD,wBAAwB,KAAG,OAAO,CAAC,8BAA8B,CAqDnE,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG;IACxC,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAAU,aAE3C,2BAA2B,KAAG,OAAO,CAAC,OAAO,CAgC/C,CAAC;AAMF,MAAM,MAAM,sBAAsB,GAAG;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,YAAY,CAAC;CACrB,CAAC;AAEF,wBAAuB,kBAAkB,CAAC,EACxC,MAAM,EACN,KAAK,EACL,KAAK,GACN,EAAE,sBAAsB,GAAG,cAAc,CAAC,cAAc,CAAC,CAQzD"}
@@ -0,0 +1,111 @@
1
+ import OpenAI from "openai";
2
+ import { retry, countWords } from "@voquill/utilities";
3
+ import { openaiCompatibleStreamChat } from "./openai.utils";
4
+ export const CEREBRAS_MODELS = [
5
+ "zai-glm-4.7",
6
+ "llama3.1-8b",
7
+ "gpt-oss-120b",
8
+ "qwen-3-235b-a22b-instruct-2507",
9
+ ];
10
+ const CEREBRAS_BASE_URL = "https://api.cerebras.ai/v1";
11
+ const contentToString = (content) => {
12
+ if (!content) {
13
+ return "";
14
+ }
15
+ if (typeof content === "string") {
16
+ return content;
17
+ }
18
+ return content
19
+ .map((part) => {
20
+ if (part.type === "text") {
21
+ return part.text ?? "";
22
+ }
23
+ return "";
24
+ })
25
+ .join("")
26
+ .trim();
27
+ };
28
+ const createClient = (apiKey) => {
29
+ return new OpenAI({
30
+ apiKey: apiKey.trim(),
31
+ baseURL: CEREBRAS_BASE_URL,
32
+ dangerouslyAllowBrowser: true,
33
+ });
34
+ };
35
+ export const cerebrasGenerateTextResponse = async ({ apiKey, model = "zai-glm-4.7", system, prompt, jsonResponse, }) => {
36
+ return retry({
37
+ retries: 3,
38
+ fn: async () => {
39
+ const client = createClient(apiKey);
40
+ const messages = [];
41
+ if (system) {
42
+ messages.push({ role: "system", content: system });
43
+ }
44
+ let finalPrompt = prompt;
45
+ if (jsonResponse) {
46
+ finalPrompt = `${prompt}\n\nRespond with valid JSON matching this schema: ${JSON.stringify(jsonResponse.schema)}`;
47
+ }
48
+ const userParts = [];
49
+ userParts.push({ type: "text", text: finalPrompt });
50
+ messages.push({ role: "user", content: userParts });
51
+ const params = {
52
+ messages,
53
+ model,
54
+ temperature: 1,
55
+ max_tokens: 1024,
56
+ top_p: 1,
57
+ response_format: jsonResponse ? { type: "json_object" } : undefined,
58
+ };
59
+ if (model === "zai-glm-4.7") {
60
+ params.reasoning_effort = "none";
61
+ }
62
+ const response = await client.chat.completions.create(params);
63
+ console.log("cerebras llm usage:", response.usage);
64
+ if (!response.choices || response.choices.length === 0) {
65
+ throw new Error("No response from Cerebras");
66
+ }
67
+ const result = response.choices[0].message.content;
68
+ if (!result) {
69
+ throw new Error("Content is empty");
70
+ }
71
+ const content = contentToString(result);
72
+ return {
73
+ text: content,
74
+ tokensUsed: response.usage?.total_tokens ?? countWords(content),
75
+ };
76
+ },
77
+ });
78
+ };
79
+ export const cerebrasTestIntegration = async ({ apiKey, }) => {
80
+ const client = createClient(apiKey);
81
+ const response = await client.chat.completions.create({
82
+ messages: [
83
+ {
84
+ role: "user",
85
+ content: [
86
+ {
87
+ type: "text",
88
+ text: `Reply with the single word "Hello."`,
89
+ },
90
+ ],
91
+ },
92
+ ],
93
+ model: "llama3.1-8b",
94
+ temperature: 0,
95
+ max_tokens: 32,
96
+ top_p: 1,
97
+ });
98
+ if (!response.choices || response.choices.length === 0) {
99
+ throw new Error("No response from Cerebras");
100
+ }
101
+ const first = response.choices[0];
102
+ const content = contentToString(first?.message?.content);
103
+ if (!content) {
104
+ throw new Error("Response content is empty");
105
+ }
106
+ return content.toLowerCase().includes("hello");
107
+ };
108
+ export async function* cerebrasStreamChat({ apiKey, model, input, }) {
109
+ const client = createClient(apiKey);
110
+ yield* openaiCompatibleStreamChat(client, model, input, model === "zai-glm-4.7" ? { reasoning_effort: "none" } : undefined);
111
+ }
package/dist/index.d.ts CHANGED
@@ -10,5 +10,6 @@ export * from "./azure.utils";
10
10
  export * from "./azure-openai.utils";
11
11
  export * from "./deepseek.utils";
12
12
  export * from "./gemini.utils";
13
+ export * from "./cerebras.utils";
13
14
  export * from "./speaches.utils";
14
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC"}
package/dist/index.js CHANGED
@@ -10,4 +10,5 @@ export * from "./azure.utils";
10
10
  export * from "./azure-openai.utils";
11
11
  export * from "./deepseek.utils";
12
12
  export * from "./gemini.utils";
13
+ export * from "./cerebras.utils";
13
14
  export * from "./speaches.utils";
@@ -43,7 +43,7 @@ export type OpenAICompatibleTestIntegrationArgs = {
43
43
  export declare const openaiCompatibleTestIntegration: ({ baseUrl, apiKey, }: OpenAICompatibleTestIntegrationArgs) => Promise<boolean>;
44
44
  export declare const openaiTestIntegration: ({ apiKey, }: OpenAITestIntegrationArgs) => Promise<boolean>;
45
45
  export declare function llmMessagesToOpenAI(messages: LlmMessage[]): ChatCompletionMessageParam[];
46
- export declare function openaiCompatibleStreamChat(client: OpenAI, model: string, input: LlmChatInput): AsyncGenerator<LlmStreamEvent>;
46
+ export declare function openaiCompatibleStreamChat(client: OpenAI, model: string, input: LlmChatInput, extraBody?: Record<string, unknown>): AsyncGenerator<LlmStreamEvent>;
47
47
  export type OpenAIStreamChatArgs = {
48
48
  apiKey: string;
49
49
  baseUrl?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"openai.utils.d.ts","sourceRoot":"","sources":["../src/openai.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EAEZ,UAAU,EACV,cAAc,EAGf,MAAM,gBAAgB,CAAC;AAExB,OAAO,MAAkB,MAAM,QAAQ,CAAC;AACxC,OAAO,KAAK,EAEV,0BAA0B,EAE3B,MAAM,mCAAmC,CAAC;AAE3C,eAAO,MAAM,2BAA2B,qGAQ9B,CAAC;AACX,MAAM,MAAM,uBAAuB,GACjC,CAAC,OAAO,2BAA2B,CAAC,CAAC,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AAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,+BAA+B,GAAU,sBAGnD,mCAAmC,KAAG,OAAO,CAAC,OAAO,CAQvD,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAAU,aAEzC,yBAAyB,KAAG,OAAO,CAAC,OAAO,CAgC7C,CAAC;AAMF,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,UAAU,EAAE,GACrB,0BAA0B,EAAE,CAyB9B;AAuCD,wBAAuB,0BAA0B,CAC/C,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,YAAY,EACnB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAClC,cAAc,CAAC,cAAc,CAAC,CAuEhC;AAED,MAAM,MAAM,oBAAoB,GAAG;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,YAAY,CAAC;IACpB,WAAW,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CACvC,CAAC;AAEF,wBAAuB,gBAAgB,CAAC,EACtC,MAAM,EACN,OAAO,EACP,KAAK,EACL,KAAK,EACL,WAAW,GACZ,EAAE,oBAAoB,GAAG,cAAc,CAAC,cAAc,CAAC,CAGvD"}
@@ -207,7 +207,7 @@ function toFinishReason(raw) {
207
207
  return "other";
208
208
  }
209
209
  }
210
- export async function* openaiCompatibleStreamChat(client, model, input) {
210
+ export async function* openaiCompatibleStreamChat(client, model, input, extraBody) {
211
211
  const stream = await client.chat.completions.create({
212
212
  model,
213
213
  messages: llmMessagesToOpenAI(input.messages),
@@ -222,6 +222,7 @@ export async function* openaiCompatibleStreamChat(client, model, input) {
222
222
  frequency_penalty: input.frequencyPenalty,
223
223
  presence_penalty: input.presencePenalty,
224
224
  seed: input.seed,
225
+ ...extraBody,
225
226
  });
226
227
  const toolCalls = new Map();
227
228
  let finishReason = "other";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voquill/voice-ai",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "Shared Groq voice transcription helpers",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,12 +22,12 @@
22
22
  "wavefile": "^11.0.0",
23
23
  "zod": "^3.25.76",
24
24
  "zod-to-json-schema": "^3.24.6",
25
- "@voquill/utilities": "0.1.3",
26
- "@voquill/types": "0.1.3"
25
+ "@voquill/types": "0.2.0",
26
+ "@voquill/utilities": "0.2.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "typescript": "5.9.2",
30
- "@voquill/typescript-config": "0.1.3"
30
+ "@voquill/typescript-config": "0.2.0"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"