Skip to content

Commit

Permalink
brain, answerAi
Browse files Browse the repository at this point in the history
  • Loading branch information
katsuhira02 committed Jul 4, 2024
1 parent 6d93cfb commit 018c719
Show file tree
Hide file tree
Showing 3 changed files with 2,275 additions and 2,266 deletions.
31 changes: 31 additions & 0 deletions supabase/functions/_shared/openai/answerAi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { openai } from "./client.ts"


export async function answerAi(
prompt: string,
language_code: string,
model: string,
) {
try {
const systemPrompt = `Reply to the user on language: ${language_code}`

const chatCompletion = await openai.chat.completions.create({
messages: [{
role: "user",
content: prompt,
}, {
role: "system",
content: systemPrompt,
}],
model,
stream: false,
temperature: 0.1,
})

console.log(chatCompletion)

return chatCompletion.choices[0].message.content;
} catch (e) {
throw new Error("Error_answerAi",e)
}
}
55 changes: 55 additions & 0 deletions supabase/functions/_shared/supabase/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,58 @@ export async function getLanguage(telegram_id: string): Promise<string | null> {
throw new Error("Error getLanguage: " + error);
}
}

export async function getMode(telegram_id: string): Promise<string> {
try {
const { data, error } = await supabase
.from("users")
.select("mode")
.eq("telegram_id", telegram_id)
.single();
if (error) {
throw new Error("Error getMode: " + error);
}
return data?.mode
} catch (e) {
throw new Error("Error getMode: " + e);
}
}

export async function setMode(telegram_id: string, mode: string): Promise<void> {
try {
const { error } = await supabase
.from("users")
.update({ mode })
.eq("telegram_id", telegram_id)
if (error) throw new Error("Error getMode: " + error);
} catch (e) {
throw new Error("Error getMode: " + e);
}
}

export async function getModel(telegram_id: string): Promise<string> {
try {
const { data, error } = await supabase
.from("users")
.select("model")
.eq("telegram_id", telegram_id)
.single();

if (error || !data) throw new Error("Error getModel: " + error)
return data?.model
} catch (error) {
throw new Error("Error getModel: " + error);
}
}

export async function setModel(telegram_id: string, model: string) {
try {
const { data, error } = await supabase
.from("users")
.update({ model })
.eq("telegram_id", telegram_id)
.select("*");
} catch (error) {
throw new Error("Error setModel: " + error);
}
}
Loading

0 comments on commit 018c719

Please sign in to comment.