llm-proxy 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +3 -2
- package/dist/index.js +52 -37
- package/dist/index.js.map +1 -1
- package/dist/types/index.d.ts +0 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Messages, SupportedLLMs,
|
|
1
|
+
import { Messages, SupportedLLMs, OpenAIResponse } from "./types";
|
|
2
2
|
interface Credentials {
|
|
3
3
|
apiKey?: string;
|
|
4
4
|
awsConfig?: {
|
|
@@ -7,5 +7,6 @@ interface Credentials {
|
|
|
7
7
|
region: string;
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
|
-
export declare function generateLLMResponse(messages: Messages, model: SupportedLLMs, maxTokens: number, temperature: number, systemPrompt: string, tools: any,
|
|
10
|
+
export declare function generateLLMResponse(messages: Messages, model: SupportedLLMs, maxTokens: number, temperature: number, systemPrompt: string, tools: any, credentials: Credentials): Promise<OpenAIResponse>;
|
|
11
|
+
export declare function generateLLMStreamResponse(messages: Messages, model: SupportedLLMs, maxTokens: number, temperature: number, systemPrompt: string, tools: any, credentials: Credentials): Promise<AsyncGenerator<OpenAIResponse>>;
|
|
11
12
|
export * from "./types";
|
package/dist/index.js
CHANGED
|
@@ -44,15 +44,16 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
|
|
|
44
44
|
};
|
|
45
45
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
46
|
exports.generateLLMResponse = generateLLMResponse;
|
|
47
|
+
exports.generateLLMStreamResponse = generateLLMStreamResponse;
|
|
47
48
|
const ProviderFinder_1 = require("./middleware/ProviderFinder");
|
|
48
49
|
const InputFormatAdapter_1 = require("./middleware/InputFormatAdapter");
|
|
49
50
|
const OutputFormatAdapter_1 = require("./middleware/OutputFormatAdapter");
|
|
50
51
|
const AwsBedrockAnthropicService_1 = require("./services/AwsBedrockAnthropicService");
|
|
51
52
|
const OpenAIService_1 = require("./services/OpenAIService");
|
|
52
53
|
const types_1 = require("./types");
|
|
53
|
-
// Main function
|
|
54
|
-
function generateLLMResponse(
|
|
55
|
-
return __awaiter(this,
|
|
54
|
+
// Main function for non-streaming requests
|
|
55
|
+
function generateLLMResponse(messages, model, maxTokens, temperature, systemPrompt, tools, credentials) {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
57
|
// Step 2: Identify the provider based on the model
|
|
57
58
|
const provider = ProviderFinder_1.ProviderFinder.getProvider(model);
|
|
58
59
|
// Initialize the correct service based on the provider
|
|
@@ -77,52 +78,66 @@ function generateLLMResponse(messages_1, model_1, maxTokens_1, temperature_1, sy
|
|
|
77
78
|
const adaptedMessages = provider !== types_1.Providers.OPENAI
|
|
78
79
|
? InputFormatAdapter_1.InputFormatAdapter.adaptMessages(messages, provider)
|
|
79
80
|
: messages;
|
|
80
|
-
// Step 4: Process the response depending on whether streaming is requested
|
|
81
|
-
if (stream) {
|
|
82
|
-
return handleStreamResponse(service, adaptedMessages, model, maxTokens, temperature, systemPrompt, tools, provider);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
return handleNonStreamResponse(service, adaptedMessages, model, maxTokens, temperature, systemPrompt, tools, provider);
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
// Helper for non-streaming response
|
|
90
|
-
function handleNonStreamResponse(service, messages, model, maxTokens, temperature, systemPrompt, tools, provider) {
|
|
91
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
92
81
|
const response = yield service.generateCompletion(provider === types_1.Providers.OPENAI
|
|
93
82
|
? messages
|
|
94
83
|
: messages, model, maxTokens, temperature, systemPrompt, tools);
|
|
95
|
-
//
|
|
84
|
+
// Adapt the response if provider is not OpenAI
|
|
96
85
|
return provider === types_1.Providers.OPENAI
|
|
97
86
|
? response
|
|
98
87
|
: OutputFormatAdapter_1.OutputFormatAdapter.adaptResponse(response, provider);
|
|
99
88
|
});
|
|
100
89
|
}
|
|
101
|
-
//
|
|
102
|
-
function
|
|
103
|
-
return
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// Step 7: Yield adapted chunks if the provider is not OpenAI
|
|
110
|
-
for (var _d = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield __await(stream_1.next()), _a = stream_1_1.done, !_a; _d = true) {
|
|
111
|
-
_c = stream_1_1.value;
|
|
112
|
-
_d = false;
|
|
113
|
-
const chunk = _c;
|
|
114
|
-
yield yield __await(provider === types_1.Providers.OPENAI
|
|
115
|
-
? chunk
|
|
116
|
-
: OutputFormatAdapter_1.OutputFormatAdapter.adaptResponse(chunk, provider));
|
|
90
|
+
// Main function for streaming requests
|
|
91
|
+
function generateLLMStreamResponse(messages, model, maxTokens, temperature, systemPrompt, tools, credentials) {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const provider = ProviderFinder_1.ProviderFinder.getProvider(model);
|
|
94
|
+
let service;
|
|
95
|
+
if (provider === types_1.Providers.OPENAI) {
|
|
96
|
+
if (!credentials.apiKey) {
|
|
97
|
+
throw new Error("OpenAI API key is required for OpenAI models.");
|
|
117
98
|
}
|
|
99
|
+
service = new OpenAIService_1.OpenAIService(credentials.apiKey);
|
|
118
100
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
101
|
+
else if (provider === types_1.Providers.ANTHROPIC_BEDROCK) {
|
|
102
|
+
const awsConfig = credentials.awsConfig;
|
|
103
|
+
if (!awsConfig) {
|
|
104
|
+
throw new Error("AWS credentials are required for Bedrock models.");
|
|
123
105
|
}
|
|
124
|
-
|
|
106
|
+
service = new AwsBedrockAnthropicService_1.AwsBedrockAnthropicService(awsConfig.accessKeyId, awsConfig.secretAccessKey, awsConfig.region);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
throw new Error("Unsupported provider");
|
|
110
|
+
}
|
|
111
|
+
const adaptedMessages = provider !== types_1.Providers.OPENAI
|
|
112
|
+
? InputFormatAdapter_1.InputFormatAdapter.adaptMessages(messages, provider)
|
|
113
|
+
: messages;
|
|
114
|
+
const stream = service.generateStreamCompletion(provider === types_1.Providers.OPENAI
|
|
115
|
+
? messages
|
|
116
|
+
: messages, model, maxTokens, temperature, systemPrompt, tools, true);
|
|
117
|
+
// Create and return the async generator
|
|
118
|
+
function streamGenerator() {
|
|
119
|
+
return __asyncGenerator(this, arguments, function* streamGenerator_1() {
|
|
120
|
+
var _a, e_1, _b, _c;
|
|
121
|
+
try {
|
|
122
|
+
for (var _d = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield __await(stream_1.next()), _a = stream_1_1.done, !_a; _d = true) {
|
|
123
|
+
_c = stream_1_1.value;
|
|
124
|
+
_d = false;
|
|
125
|
+
const chunk = _c;
|
|
126
|
+
yield yield __await(provider === types_1.Providers.OPENAI
|
|
127
|
+
? chunk
|
|
128
|
+
: OutputFormatAdapter_1.OutputFormatAdapter.adaptResponse(chunk, provider));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
132
|
+
finally {
|
|
133
|
+
try {
|
|
134
|
+
if (!_d && !_a && (_b = stream_1.return)) yield __await(_b.call(stream_1));
|
|
135
|
+
}
|
|
136
|
+
finally { if (e_1) throw e_1.error; }
|
|
137
|
+
}
|
|
138
|
+
});
|
|
125
139
|
}
|
|
140
|
+
return streamGenerator();
|
|
126
141
|
});
|
|
127
142
|
}
|
|
128
143
|
__exportStar(require("./types"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqBA,
|
|
1
|
+
{"version":3,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,CAAC,MAAM,CACjB,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QAC1C,CAAC;QAED,MAAM,eAAe,GACnB,QAAQ,KAAK,iBAAS,CAAC,MAAM;YAC3B,CAAC,CAAC,uCAAkB,CAAC,aAAa,CAAC,QAAQ,EAAE,QAAQ,CAAC;YACtD,CAAC,CAAC,QAAQ,CAAC;QAEf,MAAM,MAAM,GAAG,OAAO,CAAC,wBAAwB,CAC7C,QAAQ,KAAK,iBAAS,CAAC,MAAM;YAC3B,CAAC,CAAE,QAA2B;YAC9B,CAAC,CAAE,QAA4C,EACjD,KAAK,EACL,SAAS,EACT,WAAW,EACX,YAAY,EACZ,KAAK,EACL,IAAI,CACL,CAAC;QAEF,wCAAwC;QACxC,SAAgB,eAAe;;;;oBAC7B,KAA0B,eAAA,WAAA,cAAA,MAAM,CAAA,YAAA,qFAAE,CAAC;wBAAT,sBAAM;wBAAN,WAAM;wBAArB,MAAM,KAAK,KAAA,CAAA;wBACpB,oBAAM,QAAQ,KAAK,iBAAS,CAAC,MAAM;4BACjC,CAAC,CAAE,KAAwB;4BAC3B,CAAC,CAAE,yCAAmB,CAAC,aAAa,CAChC,KAAK,EACL,QAAQ,CACU,CAAA,CAAC;oBAC3B,CAAC;;;;;;;;;YACH,CAAC;SAAA;QAED,OAAO,eAAe,EAAE,CAAC;IAC3B,CAAC;CAAA;AAED,0CAAwB"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -184,7 +184,6 @@ export type BedrockAnthropicParsedChunk = {
|
|
|
184
184
|
};
|
|
185
185
|
export type Messages = OpenAIMessages | BedrockAnthropicMessages;
|
|
186
186
|
export type LLMResponse = OpenAIResponse | BedrockAnthropicResponse;
|
|
187
|
-
export type ProxyResponse = OpenAIResponse;
|
|
188
187
|
export type SupportedLLMs = {
|
|
189
188
|
type: "OpenAI";
|
|
190
189
|
model: OpenAISupportedLLMs;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "llm-proxy",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "An LLM Proxy that allows the user to interact with different language models from different providers using unified request and response formats.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|