|
| 1 | +import { CypherDriver } from "@graphscope/studio-driver"; |
| 2 | +const DriverMap = new Map(); |
| 3 | + |
| 4 | +export const getDriver = () => { |
| 5 | + const endpoint = "neo4j://127.0.0.1:7687"; |
| 6 | + const username = "admin"; |
| 7 | + const password = "admin"; |
| 8 | + const id = `${endpoint}:${username}:${password}`; |
| 9 | + if (!DriverMap.has(id)) { |
| 10 | + DriverMap.set(id, new CypherDriver(endpoint, username, password)); |
| 11 | + } |
| 12 | + return DriverMap.get(id); |
| 13 | +}; |
| 14 | + |
| 15 | +export function colorize(text: any, colorCode: number) { |
| 16 | + return `\x1b[${colorCode}m${text}\x1b[0m`; |
| 17 | +} |
| 18 | + |
| 19 | +export const models = [ |
| 20 | + { |
| 21 | + name: "qwen-plus", |
| 22 | + endpoint: |
| 23 | + "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions", |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "deepseek-chat", |
| 27 | + endpoint: "https://api.deepseek.com/chat/completions", |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "gpt-4o-mini", |
| 31 | + endpoint: "https://api.openai.com/v1/chat/completions", |
| 32 | + }, |
| 33 | +]; |
| 34 | + |
| 35 | +interface Base { |
| 36 | + status?: "pending" | "success" | "cancel"; |
| 37 | + role?: "system" | "assistant" | "user"; |
| 38 | + content?: string; |
| 39 | + timestamp?: number; |
| 40 | + reserved?: boolean; |
| 41 | +} |
| 42 | + |
| 43 | +export class Message implements Base { |
| 44 | + status: "pending" | "success" | "cancel"; |
| 45 | + |
| 46 | + role: "system" | "assistant" | "user"; |
| 47 | + |
| 48 | + content: string; |
| 49 | + |
| 50 | + timestamp: number; |
| 51 | + |
| 52 | + reserved: boolean; |
| 53 | + |
| 54 | + constructor(props: Partial<Base>) { |
| 55 | + this.status = props.status || "pending"; |
| 56 | + this.role = props.role || "user"; |
| 57 | + this.content = props.content || ""; |
| 58 | + this.timestamp = props.timestamp || Date.now(); |
| 59 | + this.reserved = props.reserved || false; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export function query( |
| 64 | + messages: Message[], |
| 65 | + callback?: (message: { content: string }) => void, |
| 66 | + signal?: AbortSignal |
| 67 | +): Promise<{ |
| 68 | + status: "success" | "cancel" | "failed"; |
| 69 | + message: any; |
| 70 | +}> { |
| 71 | + const model = models[0].name; |
| 72 | + const { endpoint, name } = models.find((m) => m.name === model) || models[0]; |
| 73 | + const apiKey = process.env.API_KEY; |
| 74 | + return fetch(endpoint, { |
| 75 | + signal, |
| 76 | + method: "POST", |
| 77 | + headers: { |
| 78 | + "Content-Type": "application/json", |
| 79 | + Authorization: `Bearer ${apiKey}`, |
| 80 | + }, |
| 81 | + body: JSON.stringify({ |
| 82 | + model: name, |
| 83 | + stream: false, // 启用流式返回 |
| 84 | + messages: messages.map(({ role, content }) => ({ role, content })), |
| 85 | + }), |
| 86 | + }) |
| 87 | + .then((response) => { |
| 88 | + const message = { |
| 89 | + content: "", |
| 90 | + complete: false, |
| 91 | + }; |
| 92 | + // 返回流和 Promise |
| 93 | + const stream = response.body; |
| 94 | + if (!stream) { |
| 95 | + return { |
| 96 | + status: "failed", |
| 97 | + message: { content: "Response body is missing" }, |
| 98 | + }; |
| 99 | + } |
| 100 | + const reader = response.body.getReader(); |
| 101 | + // 处理流式数据 |
| 102 | + //@ts-ignore |
| 103 | + function processStream(params: any) { |
| 104 | + const { done, value } = params; |
| 105 | + message.complete = false; |
| 106 | + if (done) { |
| 107 | + message.complete = true; |
| 108 | + return { |
| 109 | + status: "success", |
| 110 | + message: message, |
| 111 | + }; |
| 112 | + } |
| 113 | + const chunk = new TextDecoder().decode(value); |
| 114 | + chunk.split("\n").forEach((line) => { |
| 115 | + if (line.startsWith("data: ")) { |
| 116 | + const data = line.slice(6); |
| 117 | + if (data === "[DONE]") { |
| 118 | + message.complete = true; |
| 119 | + return { |
| 120 | + status: "success", |
| 121 | + message: message, |
| 122 | + }; |
| 123 | + } |
| 124 | + const parsedData = JSON.parse(data); |
| 125 | + if (parsedData.choices[0].delta.content !== undefined) { |
| 126 | + message.content = |
| 127 | + message.content + parsedData.choices[0].delta.content; |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + callback && callback(message); |
| 132 | + // 继续读取流 |
| 133 | + return reader.read().then(processStream); |
| 134 | + } |
| 135 | + return reader.read().then(processStream); |
| 136 | + }) |
| 137 | + .catch((error) => { |
| 138 | + if (error.name === "AbortError") |
| 139 | + return { |
| 140 | + status: "cancel", |
| 141 | + message: null, |
| 142 | + }; |
| 143 | + return { |
| 144 | + status: "failed", |
| 145 | + message: error.message, |
| 146 | + }; |
| 147 | + }); |
| 148 | +} |
0 commit comments