forked from Nutlope/llamacoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTogetherAIStream.ts
More file actions
113 lines (102 loc) · 3.28 KB
/
Copy pathTogetherAIStream.ts
File metadata and controls
113 lines (102 loc) · 3.28 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import {
createParser,
ParsedEvent,
ReconnectInterval,
} from "eventsource-parser";
export type ChatGPTAgent = "user" | "system";
export interface ChatGPTMessage {
role: ChatGPTAgent;
content: string;
}
export interface TogetherAIStreamPayload {
model: string;
messages: ChatGPTMessage[];
temperature: number;
stream: boolean;
}
export async function TogetherAIStream(payload: TogetherAIStreamPayload) {
const encoder = new TextEncoder();
const decoder = new TextDecoder();
let res;
if (process.env.HELICONE_API_KEY) {
res = await fetch("https://together.helicone.ai/v1/chat/completions", {
headers: {
"Content-Type": "application/json",
"Helicone-Auth": `Bearer ${process.env.HELICONE_API_KEY}`,
Authorization: `Bearer ${process.env.TOGETHER_API_KEY ?? ""}`,
},
method: "POST",
body: JSON.stringify(payload),
});
} else {
res = await fetch("https://api.together.xyz/v1/chat/completions", {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.TOGETHER_API_KEY ?? ""}`,
},
method: "POST",
body: JSON.stringify(payload),
});
}
const readableStream = new ReadableStream({
async start(controller) {
// callback
const onParse = (event: ParsedEvent | ReconnectInterval) => {
if (event.type === "event") {
const data = event.data;
controller.enqueue(encoder.encode(data));
}
};
// optimistic error handling
if (res.status !== 200) {
const data = {
status: res.status,
statusText: res.statusText,
body: await res.text(),
};
console.log(
`Error: recieved non-200 status code, ${JSON.stringify(data)}`,
);
controller.close();
return;
}
// stream response (SSE) from OpenAI may be fragmented into multiple chunks
// this ensures we properly read chunks and invoke an event for each SSE event stream
const parser = createParser(onParse);
// https://web.dev/streams/#asynchronous-iteration
for await (const chunk of res.body as any) {
parser.feed(decoder.decode(chunk));
}
},
});
let counter = 0;
const transformStream = new TransformStream({
async transform(chunk, controller) {
const data = decoder.decode(chunk);
// https://beta.openai.com/docs/api-reference/completions/create#completions/create-stream
if (data === "[DONE]") {
controller.terminate();
return;
}
try {
const json = JSON.parse(data);
const text = json.choices[0].delta?.content || "";
if (counter < 2 && (text.match(/\n/) || []).length) {
// this is a prefix character (i.e., "\n\n"), do nothing
return;
}
// stream transformed JSON resposne as SSE
const payload = { text: text };
// https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format
controller.enqueue(
encoder.encode(`data: ${JSON.stringify(payload)}\n\n`),
);
counter++;
} catch (e) {
// maybe parse error
controller.error(e);
}
},
});
return readableStream.pipeThrough(transformStream);
}