Test00_getModelBuildermain.tsx11 matches
2export async function getModelBuilder(spec: {
3type?: "llm" | "chat" | "embedding";
4provider?: "openai" | "huggingface";
5} = { type: "llm", provider: "openai" }, options?: any) {
6// 2. 使用動態導入以確保兼容性
7const { extend, cond, matches } = await import("https://esm.sh/lodash-es");
16const setup = cond([
17[
18matches({ type: "llm", provider: "openai" }),
19async () => {
20const { OpenAI } = await import("https://esm.sh/langchain/llms/openai");
21return new OpenAI(args);
22},
23],
24[
25matches({ type: "chat", provider: "openai" }),
26async () => {
27const { ChatOpenAI } = await import("https://esm.sh/langchain/chat_models/openai");
28return new ChatOpenAI(args);
29},
30],
31[
32matches({ type: "embedding", provider: "openai" }),
33async () => {
34const { OpenAIEmbeddings } = await import("https://esm.sh/langchain/embeddings/openai");
35return new OpenAIEmbeddings(args);
36},
37],
MILLENCHATmain.tsx16 matches
2// {
3// "name": "AI Chat Assistant",
4// "description": "A chat assistant using OpenAI's API",
5// "permissions": ["env"]
6// }
88}
8990async function callOpenAI(userMessage: string): Promise<string> {
91const apiKey = Deno.env.get("OPENAI_API_KEY");
92
93if (!apiKey) {
94throw new Error("OpenAI API key is not configured. Please set the OPENAI_API_KEY environment variable.");
95}
9697try {
98const response = await fetch('https://api.openai.com/v1/chat/completions', {
99method: 'POST',
100headers: {
117if (!response.ok) {
118const errorBody = await response.text();
119throw new Error(`OpenAI API error: ${response.status} - ${errorBody}`);
120}
121124"I'm not sure how to respond to that.";
125} catch (error) {
126console.error("OpenAI API Call Error:", error);
127throw error;
128}
140const [input, setInput] = useState('');
141const [isLoading, setIsLoading] = useState(false);
142const [openAIError, setOpenAIError] = useState<string | null>(null);
143const messagesEndRef = useRef<HTMLDivElement>(null);
144167setInput('');
168setIsLoading(true);
169setOpenAIError(null);
170171try {
172const botReply = await callOpenAI(userMessage);
173addMessage(botReply, 'bot');
174} catch (error) {
179: String(error);
180
181setOpenAIError(errorMessage);
182const fallbackResponse = generateFallbackResponse(userMessage);
183addMessage(`Sorry, AI service error: ${errorMessage}. Fallback response: ${fallbackResponse}`, 'bot');
195backgroundColor: '#f4f4f4'
196}}>
197{openAIError && (
198<div style={{
199backgroundColor: '#f8d7da',
203borderRadius: '5px'
204}}>
205OpenAI Error: {openAIError}
206</div>
207)}
279280export default async function server(request: Request): Promise<Response> {
281// Check if OpenAI API key is configured
282const apiKey = Deno.env.get("OPENAI_API_KEY");
283
284if (!apiKey) {
313<div class="error-container">
314<h1>🚨 Configuration Error</h1>
315<p>OpenAI API key is not configured. Please set the OPENAI_API_KEY environment variable.</p>
316<p>Contact the val owner to resolve this issue.</p>
317</div>
openai_svgmain.tsx4 matches
1import { OpenAI } from "https://esm.town/v/std/openai";
23export default async function (req: Request): Promise<Response> {
117}
118119// Generate SVG using OpenAI
120const openai = new OpenAI();
121const completion = await openai.chat.completions.create({
122messages: [
123{ role: "system", content: "You are a helpful assistant that generates simple SVG images based on text input. Return only valid SVG code wrapped in ```xml tags without any explanation." },
dreamInterpreterAppmain.tsx3 matches
68export default async function server(request: Request): Promise<Response> {
69if (request.method === "POST" && new URL(request.url).pathname === "/interpret") {
70const { OpenAI } = await import("https://esm.town/v/std/openai");
71const openai = new OpenAI();
72
73const { dream } = await request.json();
74
75try {
76const completion = await openai.chat.completions.create({
77messages: [
78{
openai_svgREADME.md1 match
1# OpenAI SVG
23Generates an SVG based on the ?text input pararm
191&& new URL(request.url).pathname === "/generate-blog"
192) {
193const { OpenAI } = await import("https://esm.town/v/std/openai");
194const openai = new OpenAI();
195196try {
199200if (topic === "RANDOM") {
201const randomTopicCompletion = await openai.chat.completions.create({
202messages: [
203{
220}
221222const completion = await openai.chat.completions.create({
223messages: [
224{
weatherGPTmain.tsx3 matches
1import { email } from "https://esm.town/v/std/email?v=11";
2import { OpenAI } from "npm:openai";
34let location = "Asia, Taipei";
8).then(r => r.json());
910const openai = new OpenAI();
11let chatCompletion = await openai.chat.completions.create({
12messages: [{
13role: "user",
getModelBuildermain.tsx14 matches
5export async function getModelBuilder(spec: {
6type?: "llm" | "chat" | "embedding";
7provider?: "openai" | "huggingface";
8} = { type: "llm", provider: "openai" }, options?: any) {
9// 3. 從 lodash-es 中導入函數
10const { extend, cond, matches, invoke } = await import("npm:lodash-es");
22// 5. 為每個提供者設置 API 密鑰
23const args = extend({ callbacks }, options);
24if (spec?.provider === "openai")
25args.openAIApiKey = process.env.OPENAI;
26else if (spec?.provider === "huggingface")
27args.apiKey = process.env.HUGGINGFACE;
30const setup = cond([
31[
32matches({ type: "llm", provider: "openai" }),
33async () => {
34const { OpenAI } = await import("npm:langchain/llms/openai");
35return new OpenAI(args);
36},
37],
38[
39matches({ type: "chat", provider: "openai" }),
40async () => {
41const { ChatOpenAI } = await import("npm:langchain/chat_models/openai");
42return new ChatOpenAI(args);
43},
44],
45[
46matches({ type: "embedding", provider: "openai" }),
47async () => {
48const { OpenAIEmbeddings } = await import(
49"npm:langchain/embeddings/openai"
50);
51return new OpenAIEmbeddings(args);
52},
53],
287export default async function server(request) {
288if (request.method === "POST" && new URL(request.url).pathname === "/chat") {
289const { OpenAI } = await import("https://esm.town/v/std/openai");
290const openai = new OpenAI();
291292try {
339}
340341const completion = await openai.chat.completions.create({
342messages: messages,
343model: imageFiles.length > 0 ? "chatgpt-4o-latest" : "gpt-4o",
350);
351} catch (error) {
352console.error("OpenAI API error:", error);
353return new Response(
354JSON.stringify({ error: "Failed to get AI response" }),
GROKPROMPTmain.tsx11 matches
526* Server-side logic for generating prompts.
527* The server expects a POST request to "/generate-prompts" with either text or image form data.
528* It uses OpenAI API to generate detailed and concise prompts.
529*/
530export default async function server(request: Request): Promise<Response> {
531if (request.method === "POST" && new URL(request.url).pathname === "/generate-prompts") {
532try {
533const { OpenAI } = await import("https://esm.town/v/std/openai");
534const openai = new OpenAI();
535const formData = await request.formData();
536const inputType = formData.get("inputType") as string;
627}
628629console.log("Sending request to OpenAI:", JSON.stringify(analysisMessages, null, 2));
630631const completion = await openai.chat.completions.create({
632model: "chatgpt-4o-latest",
633messages: analysisMessages,
635});
636637console.log("Received response from OpenAI:", JSON.stringify(completion, null, 2));
638639const content = completion.choices[0].message.content || "";
695if (request.method === "POST" && new URL(request.url).pathname === "/clarify-prompts") {
696try {
697const { OpenAI } = await import("https://esm.town/v/std/openai");
698const openai = new OpenAI();
699const { inputType, originalInput, clarificationAnswer } = await request.json();
700749const analysisMessages = [systemMessage, userMessage];
750751console.log("Sending clarification request to OpenAI:", JSON.stringify(analysisMessages, null, 2));
752753const completion = await openai.chat.completions.create({
754model: "chatgpt-4o-latest",
755messages: analysisMessages,
757});
758759console.log("Received clarification response from OpenAI:", JSON.stringify(completion, null, 2));
760761const content = completion.choices[0].message.content || "";