@@ -31,6 +31,16 @@ interface EmbeddingOptions {
31
31
}
32
32
33
33
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
+
34
44
const requestOptions = {
35
45
method : "POST" ,
36
46
headers : {
@@ -47,17 +57,12 @@ async function getRemoteEmbedding(input: string, options: EmbeddingOptions): Pro
47
57
} ;
48
58
49
59
try {
50
- const response = await fetch (
51
- `${ options . endpoint } ${ options . isOllama ? "/v1" : "" } /embeddings` ,
52
- requestOptions
53
- ) ;
60
+ const response = await fetch ( fullUrl , requestOptions ) ;
54
61
55
62
if ( ! response . ok ) {
63
+ console . error ( "API Response:" , await response . text ( ) ) ; // Debug log
56
64
throw new Error (
57
- "Embedding API Error: " +
58
- response . status +
59
- " " +
60
- response . statusText
65
+ `Embedding API Error: ${ response . status } ${ response . statusText } `
61
66
) ;
62
67
}
63
68
@@ -68,18 +73,20 @@ async function getRemoteEmbedding(input: string, options: EmbeddingOptions): Pro
68
73
const data : EmbeddingResponse = await response . json ( ) ;
69
74
return data ?. data ?. [ 0 ] . embedding ;
70
75
} catch ( e ) {
71
- console . error ( e ) ;
76
+ console . error ( "Full error details:" , e ) ;
72
77
throw e ;
73
78
}
74
79
}
75
80
81
+
76
82
/**
77
83
* Send a message to the OpenAI API for embedding.
78
84
* @param input The input to be embedded.
79
85
* @returns The embedding of the input.
80
86
*/
81
87
export async function embed ( runtime : IAgentRuntime , input : string ) {
82
88
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
83
90
const embeddingModel = modelProvider . model . embedding ;
84
91
85
92
// Try local embedding first
@@ -100,8 +107,12 @@ export async function embed(runtime: IAgentRuntime, input: string) {
100
107
// Get remote embedding
101
108
return await getRemoteEmbedding ( input , {
102
109
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
105
116
isOllama : runtime . character . modelProvider === ModelProviderName . OLLAMA && ! settings . USE_OPENAI_EMBEDDING
106
117
} ) ;
107
118
}
0 commit comments