update pay function

This commit is contained in:
2025-11-22 11:41:56 +08:00
parent a4b634abff
commit d8e4c737c5
397 changed files with 19572 additions and 9326 deletions

View File

@@ -0,0 +1,88 @@
import endent from 'endent';
import {
createParser,
ParsedEvent,
ReconnectInterval,
} from 'eventsource-parser';
const createPrompt = (inputMessage: string) => {
const data = (inputMessage: string) => {
return endent`
You are ChatGPT, but if you are asked what your name is, you present yourself as Horizon UI Boilerplate and you can be bought from your website https://horizon-ui.com/boilerplate, and the link should be bold. Also, you are very friendly and formal. The generated content must be in markdown format and it must include all markdown characteristics, but it shouldn't be rendered. The title must be bold, and there should be a   between every paragraph or title. Do not include information about console logs or print messages.
${inputMessage}
`;
};
if (inputMessage) {
return data(inputMessage);
}
};
export async function OpenAIStream (
inputMessage: string,
model: string,
key: string | undefined,
) {
const prompt = createPrompt(inputMessage);
const system = { role: 'system', content: prompt };
const res = await fetch(`https://api.openai.com/v1/chat/completions`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${key || process.env.NEXT_PUBLIC_OPENAI_API_KEY}`,
},
method: 'POST',
body: JSON.stringify({
model,
messages: [system],
temperature: 0,
stream: true,
}),
});
const encoder = new TextEncoder();
const decoder = new TextDecoder();
if (res.status !== 200) {
const statusText = res.statusText;
const result = await res.body?.getReader().read();
throw new Error(
`OpenAI API returned an error: ${
decoder.decode(result?.value) || statusText
}`,
);
}
const stream = new ReadableStream({
async start(controller) {
const onParse = (event: ParsedEvent | ReconnectInterval) => {
if (event.type === 'event') {
const data = event.data;
if (data === '[DONE]') {
controller.close();
return;
}
try {
const json = JSON.parse(data);
const text = json.choices[0].delta.content;
const queue = encoder.encode(text);
controller.enqueue(queue);
} catch (e) {
controller.error(e);
}
}
};
const parser = createParser(onParse);
for await (const chunk of res.body as any) {
parser.feed(decoder.decode(chunk));
}
},
});
return stream;
};

View File

@@ -0,0 +1,90 @@
import endent from 'endent';
import {
createParser,
ParsedEvent,
ReconnectInterval,
} from 'eventsource-parser';
const createPrompt = (
inputLanguage: string,
outputLanguage: string,
inputCode: string,
) => {
return endent`
You are an expert programmer in all programming languages.
You know very well to calculate the complexity of the code. Calculate the code complexity of the following code.
${inputCode}
`;
};
export const OpenAIStreamComplexity = async (
inputLanguage: string,
outputLanguage: string,
inputCode: string,
model: string,
key: string,
) => {
const prompt = createPrompt(inputLanguage, outputLanguage, inputCode);
const system = { role: 'system', content: prompt };
const res = await fetch(`https://api.openai.com/v1/chat/completions`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${key || process.env.OPENAI_API_KEY}`,
},
method: 'POST',
body: JSON.stringify({
model,
messages: [system],
temperature: 0,
stream: true,
}),
});
const encoder = new TextEncoder();
const decoder = new TextDecoder();
if (res.status !== 200) {
const statusText = res.statusText;
const result = await res.body?.getReader().read();
throw new Error(
`OpenAI API returned an error: ${
decoder.decode(result?.value) || statusText
}`,
);
}
const stream = new ReadableStream({
async start(controller) {
const onParse = (event: ParsedEvent | ReconnectInterval) => {
if (event.type === 'event') {
const data = event.data;
if (data === '[DONE]') {
controller.close();
return;
}
try {
const json = JSON.parse(data);
const text = json.choices[0].delta.content;
const queue = encoder.encode(text);
controller.enqueue(queue);
} catch (e) {
controller.error(e);
}
}
};
const parser = createParser(onParse);
for await (const chunk of res.body as any) {
parser.feed(decoder.decode(chunk));
}
},
});
return stream;
};

View File

@@ -0,0 +1,93 @@
import endent from 'endent';
import {
createParser,
ParsedEvent,
ReconnectInterval,
} from 'eventsource-parser';
const createPrompt = (topic: string, words: string, essayType: string) => {
const data = (topic: any, words: string, essayType: string) => {
return endent`
You are an expert formal essay writer and generator.
You know very well all types of essays. Generate an formal ${essayType} essay about ${topic}, which has a number of maximum ${words} words.
The generated content should NOT be longer than ${words} words.
The essay must be in markdown format but not rendered, it must include all markdown characteristics. The title must be bold, and there should be a   between every paragraph.
Do not include informations about console logs or print messages.
`;
};
if (essayType) {
return data(topic, words, essayType);
}
};
export const OpenAIStream = async (
topic: string,
essayType: string,
words: string,
model: string,
key: string | undefined,
) => {
const prompt = createPrompt(topic, words, essayType);
const system = { role: 'system', content: prompt };
const res = await fetch(`https://api.openai.com/v1/chat/completions`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${key || process.env.NEXT_PUBLIC_OPENAI_API_KEY}`,
},
method: 'POST',
body: JSON.stringify({
model,
messages: [system],
temperature: 0,
stream: true,
}),
});
const encoder = new TextEncoder();
const decoder = new TextDecoder();
if (res.status !== 200) {
const statusText = res.statusText;
const result = await res.body?.getReader().read();
throw new Error(
`OpenAI API returned an error: ${
decoder.decode(result?.value) || statusText
}`,
);
}
const stream = new ReadableStream({
async start(controller) {
const onParse = (event: ParsedEvent | ReconnectInterval) => {
if (event.type === 'event') {
const data = event.data;
if (data === '[DONE]') {
controller.close();
return;
}
try {
const json = JSON.parse(data);
const text = json.choices[0].delta.content;
const queue = encoder.encode(text);
controller.enqueue(queue);
} catch (e) {
controller.error(e);
}
}
};
const parser = createParser(onParse);
for await (const chunk of res.body as any) {
parser.feed(decoder.decode(chunk));
}
},
});
return stream;
};

View File

@@ -0,0 +1,118 @@
import endent from 'endent';
import {
createParser,
ParsedEvent,
ReconnectInterval,
} from 'eventsource-parser';
const createPrompt = (
inputLanguage: string,
outputLanguage: string,
inputCode: string,
) => {
const data = (inputCode: any, type: string) => {
return endent`
You are an expert programmer in all programming languages.
You know very well algorithms. You will explain how the code works.
The explanation must me in markdown format but not rendered, it must include all markdown characteristics.
Do not include informations about console logs or print messages. Explain the following code ${type !== 'persona' && 'as I am a' + type}:
${inputCode}
`;
};
switch (outputLanguage) {
case 'persona':
return data(inputCode, 'persona');
case 'teacher':
return data(inputCode, 'teacher');
case '5':
return data(inputCode, '5 years boy');
case 'beginner':
return data(inputCode, 'beginner programmer');
case 'nasa':
return data(inputCode, 'very intelligent PHD professor at MIT');
case 'pizza-delivery-guy':
return data(inputCode, 'pizza delivery guy');
case 'bus-driver':
return data(inputCode, 'bus driver');
case 'magician':
return data(inputCode, 'magician');
case 'barista':
return data(inputCode, 'barista');
case 'doctor':
return data(inputCode, 'doctor');
}
};
export const OpenAIStream = async (
inputLanguage: string,
outputLanguage: string,
inputCode: string,
model: string,
key: string,
) => {
const prompt = createPrompt(inputLanguage, outputLanguage, inputCode);
const system = { role: 'system', content: prompt };
const res = await fetch(`https://api.openai.com/v1/chat/completions`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${key || process.env.OPENAI_API_KEY}`,
},
method: 'POST',
body: JSON.stringify({
model,
messages: [system],
temperature: 0,
stream: true,
}),
});
const encoder = new TextEncoder();
const decoder = new TextDecoder();
if (res.status !== 200) {
const statusText = res.statusText;
const result = await res.body?.getReader().read();
throw new Error(
`OpenAI API returned an error: ${
decoder.decode(result?.value) || statusText
}`,
);
}
const stream = new ReadableStream({
async start(controller) {
const onParse = (event: ParsedEvent | ReconnectInterval) => {
if (event.type === 'event') {
const data = event.data;
if (data === '[DONE]') {
controller.close();
return;
}
try {
const json = JSON.parse(data);
const text = json.choices[0].delta.content;
const queue = encoder.encode(text);
controller.enqueue(queue);
} catch (e) {
controller.error(e);
}
}
};
const parser = createParser(onParse);
for await (const chunk of res.body as any) {
parser.feed(decoder.decode(chunk));
}
},
});
return stream;
};

View File

@@ -0,0 +1,100 @@
import endent from 'endent';
import {
createParser,
ParsedEvent,
ReconnectInterval,
} from 'eventsource-parser';
const createPrompt = (words:string,topic: string, essayType: string, tone:string, citation:string, level:string,citations:boolean) => {
const data = (words:string,topic: any, essayType: string, tone:string, citation:string, level:string,citations:boolean) => {
return endent`
You are an expert formal essay writer and generator.
You know very well all types of essays. Generate an ${essayType} essay about ${topic}, in ${words} words.
The generated content must have at least ${words} words.
The essay should be written on an ${tone} tone and be written for the ${level} academic level.
Also, the citation format of the essay should be ${citation}.
Throught the project, you ${citations ? "must" : "must not"} use citations, quoting books or famous people regarding the subject.
The essay must be in markdown format but not rendered, it must include all markdown characteristics.The title must be bold, and there should be a &nbsp between every paragraph.
Do not include informations about console logs or print messages.
`;
};
if (essayType) {
return data(words,topic, essayType, tone, citation, level,citations);
}
};
export const OpenAIStream = async (
words:string,
topic: string,
essayType: string,
tone: string,
citation: string,
level: string,
citations:boolean,
model: string,
key: string | undefined,
) => {
const prompt = createPrompt(words, topic, essayType, tone, citation, level, citations);
const system = { role: 'system', content: prompt };
const res = await fetch(`https://api.openai.com/v1/chat/completions`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${key || process.env.NEXT_PUBLIC_OPENAI_API_KEY}`,
},
method: 'POST',
body: JSON.stringify({
model,
messages: [system],
temperature: 0,
stream: true,
}),
});
const encoder = new TextEncoder();
const decoder = new TextDecoder();
if (res.status !== 200) {
const statusText = res.statusText;
const result = await res.body?.getReader().read();
throw new Error(
`OpenAI API returned an error: ${
decoder.decode(result?.value) || statusText
}`,
);
}
const stream = new ReadableStream({
async start(controller) {
const onParse = (event: ParsedEvent | ReconnectInterval) => {
if (event.type === 'event') {
const data = event.data;
if (data === '[DONE]') {
controller.close();
return;
}
try {
const json = JSON.parse(data);
const text = json.choices[0].delta.content;
const queue = encoder.encode(text);
controller.enqueue(queue);
} catch (e) {
controller.error(e);
}
}
};
const parser = createParser(onParse);
for await (const chunk of res.body as any) {
parser.feed(decoder.decode(chunk));
}
},
});
return stream;
};