forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
158 lines (141 loc) · 3.97 KB
/
Copy pathapi.ts
File metadata and controls
158 lines (141 loc) · 3.97 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { chatApiBase } from "./env";
export type AIModel =
| "gemini-2.5-flash"
| "gemini-2.5-pro"
| "gemini-2.0-flash"
| "gemini-flash-latest"
| "gemini-pro-latest"
| "gemini-2.0-flash-exp";
export const MODEL_CONFIG = {
"gemini-2.5-flash": {
name: "Gemini 2.5 Flash",
description: "Fast and versatile multimodal model",
inputTokenLimit: 1_048_576,
outputTokenLimit: 65_536,
},
"gemini-2.5-pro": {
name: "Gemini 2.5 Pro",
description: "Advanced reasoning and analysis capabilities",
inputTokenLimit: 1_048_576,
outputTokenLimit: 65_536,
},
"gemini-2.0-flash": {
name: "Gemini 2.0 Flash",
description: "Fast and efficient multimodal model",
inputTokenLimit: 1_048_576,
outputTokenLimit: 8_192,
},
"gemini-flash-latest": {
name: "Gemini Flash Latest",
description: "Latest release of Gemini Flash",
inputTokenLimit: 1_048_576,
outputTokenLimit: 65_536,
},
"gemini-pro-latest": {
name: "Gemini Pro Latest",
description: "Latest release of Gemini Pro",
inputTokenLimit: 1_048_576,
outputTokenLimit: 65_536,
},
"gemini-2.0-flash-exp": {
name: "Gemini 2.0 Flash Experimental",
description: "Experimental version with latest features",
inputTokenLimit: 1_048_576,
outputTokenLimit: 8_192,
},
};
async function fetchJson<T>(input: string, init?: RequestInit): Promise<T> {
const response = await fetch(input, init);
if (!response.ok) {
const text = await response.text();
throw new Error(text || `Request failed with status ${response.status}.`);
}
return response.json() as Promise<T>;
}
export async function getModels() {
try {
return await fetchJson<{ models: string[] }>(`${chatApiBase}/api/models`);
} catch {
return {
models: [
"gemini-2.5-flash",
"gemini-2.5-pro",
"gemini-2.0-flash",
"gemini-flash-latest",
],
};
}
}
export async function checkHealth() {
try {
const response = await fetch(`${chatApiBase}/api/health`);
return response.ok;
} catch {
return false;
}
}
export async function getChatResponse(
prompt: string,
model: AIModel = "gemini-2.5-flash",
) {
return fetchJson<unknown>(
`${chatApiBase}/api/chat?prompt=${encodeURIComponent(prompt)}&model=${model}`,
{
method: "GET",
headers: {
Accept: "application/json",
},
},
);
}
export function localImprovePrompt(prompt: string) {
let improved = prompt;
if (prompt.length < 20) {
improved = `${prompt} with detailed examples and step-by-step instructions`;
}
if (!prompt.includes("?") && prompt.split(" ").length < 5) {
improved = `Please provide a comprehensive explanation about ${prompt}`;
}
if (
prompt.length > 50 &&
!prompt.includes("1.") &&
!prompt.includes("First")
) {
improved = `${prompt}\n\nPlease structure your response with:\n1. Introduction\n2. Main points\n3. Examples\n4. Conclusion`;
}
if (improved === prompt) {
improved = `${prompt}\n\nPlease provide a detailed, well-structured response with examples where appropriate.`;
}
return improved;
}
export async function improvePrompt(prompt: string) {
try {
const result = await fetchJson<unknown>(`${chatApiBase}/api/improve-prompt`, {
method: "POST",
headers: {
"Content-Type": "text/plain",
Accept: "application/json",
},
body: prompt,
});
if (typeof result === "string") {
return result || localImprovePrompt(prompt);
}
if (result && typeof result === "object") {
const candidate =
"improved" in result
? result.improved
: "Response" in result
? result.Response
: "response" in result
? result.response
: null;
return typeof candidate === "string" && candidate.trim()
? candidate
: localImprovePrompt(prompt);
}
return localImprovePrompt(prompt);
} catch {
return localImprovePrompt(prompt);
}
}