Skip to content

Commit 86d8be9

Browse files
authored
Merge pull request #255 from o-on-x/main
embedding set to use openai endpoint when using openai embeddings
2 parents 420399e + ad0a45e commit 86d8be9

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

packages/core/src/embedding.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ interface EmbeddingOptions {
3131
}
3232

3333
async function getRemoteEmbedding(input: string, options: EmbeddingOptions): Promise<number[]> {
34+
// Ensure endpoint ends with /v1 for OpenAI
35+
const baseEndpoint = options.endpoint.endsWith('/v1') ?
36+
options.endpoint :
37+
`${options.endpoint}${options.isOllama ? '/v1' : ''}`;
38+
39+
// Construct full URL
40+
const fullUrl = `${baseEndpoint}/embeddings`;
41+
42+
console.log("Calling embedding API at:", fullUrl); // Debug log
43+
3444
const requestOptions = {
3545
method: "POST",
3646
headers: {
@@ -47,17 +57,12 @@ async function getRemoteEmbedding(input: string, options: EmbeddingOptions): Pro
4757
};
4858

4959
try {
50-
const response = await fetch(
51-
`${options.endpoint}${options.isOllama ? "/v1" : ""}/embeddings`,
52-
requestOptions
53-
);
60+
const response = await fetch(fullUrl, requestOptions);
5461

5562
if (!response.ok) {
63+
console.error("API Response:", await response.text()); // Debug log
5664
throw new Error(
57-
"Embedding API Error: " +
58-
response.status +
59-
" " +
60-
response.statusText
65+
`Embedding API Error: ${response.status} ${response.statusText}`
6166
);
6267
}
6368

@@ -68,18 +73,20 @@ async function getRemoteEmbedding(input: string, options: EmbeddingOptions): Pro
6873
const data: EmbeddingResponse = await response.json();
6974
return data?.data?.[0].embedding;
7075
} catch (e) {
71-
console.error(e);
76+
console.error("Full error details:", e);
7277
throw e;
7378
}
7479
}
7580

81+
7682
/**
7783
* Send a message to the OpenAI API for embedding.
7884
* @param input The input to be embedded.
7985
* @returns The embedding of the input.
8086
*/
8187
export async function embed(runtime: IAgentRuntime, input: string) {
8288
const modelProvider = models[runtime.character.modelProvider];
89+
//need to have env override for this to select what to use for embedding if provider doesnt provide or using openai
8390
const embeddingModel = modelProvider.model.embedding;
8491

8592
// Try local embedding first
@@ -100,8 +107,12 @@ export async function embed(runtime: IAgentRuntime, input: string) {
100107
// Get remote embedding
101108
return await getRemoteEmbedding(input, {
102109
model: embeddingModel,
103-
endpoint: runtime.character.modelEndpointOverride || modelProvider.endpoint,
104-
apiKey: runtime.token,
110+
endpoint: settings.USE_OPENAI_EMBEDDING ?
111+
'https://api.openai.com/v1' : // Always use OpenAI endpoint when USE_OPENAI_EMBEDDING is true
112+
(runtime.character.modelEndpointOverride || modelProvider.endpoint),
113+
apiKey: settings.USE_OPENAI_EMBEDDING ?
114+
settings.OPENAI_API_KEY : // Use OpenAI key from settings when USE_OPENAI_EMBEDDING is true
115+
runtime.token, // Use runtime token for other providers
105116
isOllama: runtime.character.modelProvider === ModelProviderName.OLLAMA && !settings.USE_OPENAI_EMBEDDING
106117
});
107118
}

0 commit comments

Comments
 (0)