generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
083de0e
commit fc8b53d
Showing
8 changed files
with
243 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const generateConfig = (document: string, filePaths: string, model: string) => { | ||
console.log("generateConfig", filePaths); | ||
const config = { | ||
"gpt-3.5-turbo": { | ||
url: "https://api.openai.com/v1/chat/completions", | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: | ||
"Always answer with the full path of the most appropriate file from the provided list. If none of the files are suitable, answer with 'None'.", | ||
}, | ||
{ | ||
role: "user", | ||
content: `Given the document: "${document}", which of the following files best matches the user's request? Available files: ${filePaths}`, | ||
}, | ||
], | ||
}, | ||
"dolphin-mistral": { | ||
url: "http://localhost:11434/v1/chat/completions", | ||
messages: [ | ||
{ | ||
role: "assistant", | ||
content: `Always answer full filename. Example Answer: "folder/subfolder/file.md"`, | ||
}, | ||
{ | ||
role: "user", | ||
content: ` | ||
document: "${document}", | ||
files: "${filePaths}", | ||
identify the most relevant file. If none are suitable, respond with 'None'.`, | ||
}, | ||
], | ||
}, | ||
llama3: { | ||
url: "http://localhost:11434/v1/chat/completions", | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: `Based on the document: "${document}", identify the most relevant file. If none are suitable, respond with 'None'.`, | ||
}, | ||
], | ||
}, | ||
}; | ||
return config[model]; | ||
}; | ||
|
||
export async function POST(request: Request) { | ||
console.log("received post on files route"); | ||
try { | ||
const apiKey = process.env.OPENAI_API_KEY || ""; | ||
const useOllama = process.env.USE_OLLAMA === "true"; | ||
const requestBody = await request.json(); | ||
const model = useOllama ? "dolphin-mistral" : "gpt-3.5-turbo"; | ||
|
||
const config = generateConfig( | ||
requestBody.document, | ||
requestBody.filePaths.join(", "), | ||
model | ||
); | ||
|
||
const data = { | ||
model, | ||
messages: [...config.messages], | ||
}; | ||
|
||
console.log("file data", data); | ||
const response = await fetch(config.url, { | ||
method: "POST", | ||
body: JSON.stringify(data), | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
}); | ||
|
||
const result = await response.json(); | ||
console.log("magic", result.choices[0].message.content); | ||
return new Response(JSON.stringify(result), { | ||
status: 200, | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
return new Response(JSON.stringify({ message: "Error" }), { | ||
status: 500, | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const generateConfig = (document: string, folders: string[], model) => { | ||
const config = { | ||
"gpt-3.5-turbo": { | ||
url: "https://api.openai.com/v1/chat/completions", | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: | ||
"Always answer with the name of the most appropriate folder from the provided list. If none of the folders are suitable, answer with 'None'.", | ||
}, | ||
{ | ||
role: "user", | ||
content: `Given the text "${document}", which of the following folders would be the most appropriate location for the file? Available folders: ${folders}`, | ||
}, | ||
], | ||
}, | ||
"dolphin-mistral": { | ||
url: "http://localhost:11434/v1/chat/completions", | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: | ||
"Always answer with the name of the most appropriate folder from the provided list. If none of the folders are suitable, answer with 'None'.", | ||
}, | ||
{ | ||
role: "user", | ||
content: `Given the text "${document}", which of the following folders would be the most appropriate location for the file? Available folders: ${folders}`, | ||
}, | ||
], | ||
}, | ||
llama3: { | ||
url: "http://localhost:11434/v1/chat/completions", | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: | ||
"Always answer with the name of the most appropriate folder from the provided list. If none of the folders are suitable, answer with 'None'.", | ||
}, | ||
{ | ||
role: "user", | ||
content: `Given the text "${document}", which of the following folders would be the most appropriate location for the file? Available folders: ${folders}`, | ||
}, | ||
], | ||
}, | ||
}; | ||
return config[model]; | ||
}; | ||
|
||
export async function POST(request: Request) { | ||
console.log("received post on folders route"); | ||
try { | ||
const apiKey = process.env.OPENAI_API_KEY || ""; | ||
const useOllama = process.env.USE_OLLAMA === "true"; | ||
const requestBody = await request.json(); | ||
const model = useOllama ? "dolphin-mistral" : "gpt-3.5-turbo"; | ||
|
||
const config = generateConfig( | ||
requestBody.document, | ||
requestBody.folders.join(", "), | ||
model | ||
); | ||
|
||
const data = { | ||
model, | ||
messages: [...config.messages], | ||
}; | ||
|
||
console.log("folder data", data); | ||
const response = await fetch(config.url, { | ||
method: "POST", | ||
body: JSON.stringify(data), | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
}); | ||
if (response.status === 401) { | ||
return new Response(JSON.stringify({ message: "Invalid API key" }), { | ||
status: 401, | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
} | ||
const result = await response.json(); | ||
console.log("result from folders", result); | ||
return new Response(JSON.stringify(result), { | ||
status: 200, | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
return new Response(JSON.stringify({ message: "Error" }), { | ||
status: 500, | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { requestUrl } from "obsidian"; | ||
import { logMessage } from "../../utils"; | ||
|
||
async function predictMostSimilarFile( | ||
content: string, | ||
allMarkdownFilePaths: string[], | ||
{ baseUrl, apiKey } | ||
) { | ||
const endpoint = "api/files"; | ||
const url = `${baseUrl}/${endpoint}`; | ||
|
||
const response = await requestUrl({ | ||
url: url, | ||
method: "POST", | ||
body: JSON.stringify({ | ||
document: content, | ||
filePaths: allMarkdownFilePaths, | ||
}), | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
}); | ||
const result = await response.json; | ||
logMessage(`Most similar file path: ${result.choices[0].message.content}`); | ||
return result.choices[0].message.content.trim(); | ||
} | ||
|
||
export default predictMostSimilarFile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters