mirror of
https://github.com/yzh503/obsidian-aicommander-plugin.git
synced 2026-07-22 07:40:26 +00:00
Fix search result
This commit is contained in:
parent
c7248e6957
commit
ae1a176893
1 changed files with 8 additions and 19 deletions
27
src/main.ts
27
src/main.ts
|
|
@ -75,15 +75,15 @@ export default class AICommanderPlugin extends Plugin {
|
||||||
|
|
||||||
if (contextPrompt) {
|
if (contextPrompt) {
|
||||||
messages.push({
|
messages.push({
|
||||||
role: 'system',
|
role: 'user',
|
||||||
content: contextPrompt
|
content: contextPrompt
|
||||||
});
|
});
|
||||||
} else if (this.settings.useSearchEngine) {
|
} else if (this.settings.useSearchEngine) {
|
||||||
if (this.settings.bingSearchKey.length <= 1) throw new Error('Bing Search API Key is not provided.');
|
if (this.settings.bingSearchKey.length <= 1) throw new Error('Bing Search API Key is not provided.');
|
||||||
const searchResult = await this.searchText(prompt)
|
const searchResult = await this.searchText(prompt)
|
||||||
messages.push({
|
messages.push({
|
||||||
role: 'system',
|
role: 'user',
|
||||||
content: 'As an assistant who can learn information from web search results, your task is to incorporate information from a web search API JSON response into your answers when responding to questions. Your response should include the relevant information from the JSON and provide attribution by mentioning the source of information with its url in the format of markdown. Please note that you should be able to handle various types of questions and search queries. Your response should also be clear and concise while incorporating all relevant information from the web search results. Here are the web search API response in JSON format: \n\n ' + JSON.stringify(searchResult)
|
content: 'As an assistant who can answer questions using information from web search results, your task is to incorporate information from a web search results into your answers when responding to questions. Your response should include the relevant information from the search result and provide attribution by mentioning the source of information with its url in the format of markdown. Here are the web search results: \n\n ' + JSON.stringify(searchResult)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -97,7 +97,9 @@ export default class AICommanderPlugin extends Plugin {
|
||||||
messages: messages as ChatCompletionRequestMessage[],
|
messages: messages as ChatCompletionRequestMessage[],
|
||||||
};
|
};
|
||||||
|
|
||||||
const completion = await openai.createChatCompletion(data)
|
console.log(data);
|
||||||
|
|
||||||
|
const completion = await openai.createChatCompletion(data).catch((error) => {throw new Error(error.message)});
|
||||||
|
|
||||||
const message = completion.data.choices[0].message
|
const message = completion.data.choices[0].message
|
||||||
if (!message) throw new Error('No response from OpenAI API');
|
if (!message) throw new Error('No response from OpenAI API');
|
||||||
|
|
@ -203,16 +205,12 @@ export default class AICommanderPlugin extends Plugin {
|
||||||
else throw new Error('No transcript received: ' + JSON.stringify(response.json));
|
else throw new Error('No transcript received: ' + JSON.stringify(response.json));
|
||||||
}
|
}
|
||||||
|
|
||||||
async searchText(prompt: string) {
|
async searchText(query: string) {
|
||||||
|
|
||||||
const params = {
|
const params = {
|
||||||
url: 'https://api.bing.microsoft.com/v7.0/search',
|
url: 'https://api.bing.microsoft.com/v7.0/search?q=' + encodeURIComponent(query),
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
contentType: 'application/json',
|
contentType: 'application/json',
|
||||||
body: JSON.stringify({
|
|
||||||
q: prompt,
|
|
||||||
count: 20
|
|
||||||
}),
|
|
||||||
headers: {
|
headers: {
|
||||||
'Ocp-Apim-Subscription-Key': this.settings.bingSearchKey
|
'Ocp-Apim-Subscription-Key': this.settings.bingSearchKey
|
||||||
}
|
}
|
||||||
|
|
@ -353,9 +351,6 @@ export default class AICommanderPlugin extends Plugin {
|
||||||
new Notice(`Generating text in context of ${path}...`);
|
new Notice(`Generating text in context of ${path}...`);
|
||||||
this.generateTextWithPdf(prompt, path).then((data) => {
|
this.generateTextWithPdf(prompt, path).then((data) => {
|
||||||
this.processGeneratedText(editor, data, lineToInsert);
|
this.processGeneratedText(editor, data, lineToInsert);
|
||||||
}).catch(error => {
|
|
||||||
console.log(error.message);
|
|
||||||
new Notice(error.message);
|
|
||||||
});
|
});
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
console.log(error.message);
|
console.log(error.message);
|
||||||
|
|
@ -395,13 +390,7 @@ export default class AICommanderPlugin extends Plugin {
|
||||||
new Notice('Transcript Generated.');
|
new Notice('Transcript Generated.');
|
||||||
editor.setLine(position.line, `${line}${result}\n`);
|
editor.setLine(position.line, `${line}${result}\n`);
|
||||||
});
|
});
|
||||||
}).catch(error => {
|
|
||||||
console.log(error.message);
|
|
||||||
new Notice(error.message);
|
|
||||||
});
|
});
|
||||||
}).catch(error => {
|
|
||||||
console.log(error.message);
|
|
||||||
new Notice(error.message);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue