Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inference job failed #424 #445

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions apps/workers/inference.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Ollama } from "ollama";
import OpenAI from "openai";
import { ChatCompletion } from "openai/resources/chat/completions";

import serverConfig from "@hoarder/shared/config";
import logger from "@hoarder/shared/logger";
Expand Down Expand Up @@ -41,17 +42,32 @@ class OpenAIInferenceClient implements InferenceClient {
});
}

/**
* @param chatCompletion the chatCompletion object or the chatCompletion object as a string (e.g. when using OpenRouter in some occasions)
* @returns the messageContent extracted out of the chatCompletion object or out of the chatCompletion string
*/
extractResponse(chatCompletion: ChatCompletion | string): string {
let response: ChatCompletion;
if (typeof chatCompletion === "string") {
response = JSON.parse(chatCompletion) as ChatCompletion;
} else {
response = chatCompletion;
}
const responseContent = response.choices[0].message.content;
if (!responseContent) {
throw new Error(`Got no message content from OpenAI`);
}
return responseContent;
}

async inferFromText(prompt: string): Promise<InferenceResponse> {
const chatCompletion = await this.openAI.chat.completions.create({
messages: [{ role: "system", content: prompt }],
model: serverConfig.inference.textModel,
response_format: { type: "json_object" },
});

const response = chatCompletion.choices[0].message.content;
if (!response) {
throw new Error(`Got no message content from OpenAI`);
}
const response = this.extractResponse(chatCompletion);
return { response, totalTokens: chatCompletion.usage?.total_tokens };
}

Expand Down Expand Up @@ -81,10 +97,7 @@ class OpenAIInferenceClient implements InferenceClient {
max_tokens: 2000,
});

const response = chatCompletion.choices[0].message.content;
if (!response) {
throw new Error(`Got no message content from OpenAI`);
}
const response = this.extractResponse(chatCompletion);
return { response, totalTokens: chatCompletion.usage?.total_tokens };
}
}
Expand Down
Loading