Skip to content

Commit

Permalink
fix api usage
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Mar 3, 2024
1 parent 8a50eef commit 230c657
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
15 changes: 13 additions & 2 deletions src/ai/OpenAIRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { settingsStore } from "src/settingsStore";
import { getTokenCount } from "./AIAssistant";
import { preventCursorChange } from "./preventCursorChange";
import type { Model } from "./Provider";
import { getModelProvider } from "./aiHelpers";

type ReqResponse = {
id: string;
Expand Down Expand Up @@ -45,17 +46,27 @@ export function OpenAIRequest(
);
}

const modelProvider = getModelProvider(model.name);

if (!modelProvider) {
throw new Error(`Model ${model.name} not found with any provider.`);
}

console.log(
`Making request to ${modelProvider?.name} at ${modelProvider.endpoint} with model ${model.name}`
);

try {
const restoreCursor = preventCursorChange();
const _response = requestUrl({
url: `https://api.openai.com/v1/chat/completions`,
url: `${modelProvider?.endpoint}/chat/completions`,
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
model,
model: model.name,
...modelParams,
messages: [
{ role: "system", content: systemPrompt },
Expand Down
20 changes: 16 additions & 4 deletions src/engine/MacroChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ import type { IAIAssistantCommand } from "src/types/macros/QuickCommands/IAIAssi
import { runAIAssistant } from "src/ai/AIAssistant";
import { settingsStore } from "src/settingsStore";
import { CompleteFormatter } from "src/formatters/completeFormatter";
import { getModelNames, getModelProvider } from "src/ai/aiHelpers";
import {
getModelByName,
getModelNames,
getModelProvider,
} from "src/ai/aiHelpers";
import { Model } from "src/ai/Provider";

export class MacroChoiceEngine extends QuickAddChoiceEngine {
public choice: IMacroChoice;
Expand Down Expand Up @@ -306,21 +311,28 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
const aiSettings = settingsStore.getState().ai;

const options = [...getModelNames()];
const model =
const modelName: string =
command.model === "Ask me"
? await GenericSuggester.Suggest(app, options, options)
: command.model;

const model: Model | undefined = getModelByName(modelName);

if (!model) {
throw new Error(`Model ${modelName} not found with any provider.`);
}

const formatter = new CompleteFormatter(
app,
QuickAdd.instance,
this.choiceExecutor
);

const modelProvider = getModelProvider(model);
const modelProvider = getModelProvider(model.name);

if (!modelProvider) {
throw new Error(
`Model ${model} not found in the AI providers settings.`
`Model ${model.name} not found in the AI providers settings.`
);
}

Expand Down

0 comments on commit 230c657

Please sign in to comment.