Skip to content

Instantly share code, notes, and snippets.

@sheepla
Created April 20, 2024 07:13
Show Gist options
  • Save sheepla/e55a8ee3f45f53c10ca3c55e29cc0d95 to your computer and use it in GitHub Desktop.
Save sheepla/e55a8ee3f45f53c10ca3c55e29cc0d95 to your computer and use it in GitHub Desktop.
import { TextLineStream } from "https://deno.land/std@0.223.0/streams/text_line_stream.ts";
import * as bufio from "https://deno.land/std@0.101.0/io/bufio.ts";
type AiModel = "gpt-3.5-turbo-0125" | "claude-instant-1.2";
export async function isStatusOK(): Promise<boolean> {
return await fetch("https://duckduckgo.com/duckchat/v1/status")
.then((response) => response.json()).then((data) =>
parseInt(data["status"]) === 0
);
}
async function fetchChatResponseStream(
prompt: string,
model: AiModel,
vqd4: string,
): Promise<ReadableStream<string>> {
const payload = {
"model": model,
"messages": [{ "role": "user", "content": prompt }],
};
const headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
"Accept": "text/event-stream",
"Accept-Language": "ja,en-US;q=0.7,en;q=0.3",
"Content-Type": "application/json",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Sec-GPC": "1",
"sec-ch-ua-platform": '"Windows"',
"sec-ch-ua":
'"Google Chrome";v="118", "Chromium";v="118", "Not=A?Brand";v="24"',
"sec-ch-ua-mobile": "?0",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"x-vqd-4": vqd4,
};
return await fetch("https://duckduckgo.com/duckchat/v1/chat", {
"method": "POST",
"credentials": "include",
"headers": headers,
"referrer": "https://duckduckgo.com/",
"mode": "cors",
"body": JSON.stringify(payload),
}).then((response: Response) => {
const stream = response.body!
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
return stream;
});
}
async function printChatResponse(
reader: ReadableStreamDefaultReader<string>,
) {
const buf = bufio.BufWriter.create(Deno.stdout);
const encoder = new TextEncoder();
while (true) {
const result: ReadableStreamDefaultReadResult<string> = await reader.read();
const line: string = result.value?.replace("data: ", "")!;
if (result.done || line.startsWith("[DONE]")) {
break;
}
console.debug(line);
//const json = JSON.parse(line);
//console.debug(json);
//buf.write(encoder.encode(json["message"]));
buf.write(encoder.encode(" "));
}
await buf.flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment