@loonylabs/llm-middleware 2.0.0 โ 2.1.0
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/.env.example +4 -0
- package/README.md +34 -7
- package/dist/middleware/services/llm/llm.service.d.ts.map +1 -1
- package/dist/middleware/services/llm/llm.service.js +2 -0
- package/dist/middleware/services/llm/llm.service.js.map +1 -1
- package/dist/middleware/services/llm/providers/anthropic-provider.d.ts +28 -0
- package/dist/middleware/services/llm/providers/anthropic-provider.d.ts.map +1 -0
- package/dist/middleware/services/llm/providers/anthropic-provider.js +275 -0
- package/dist/middleware/services/llm/providers/anthropic-provider.js.map +1 -0
- package/dist/middleware/services/llm/providers/index.d.ts +1 -0
- package/dist/middleware/services/llm/providers/index.d.ts.map +1 -1
- package/dist/middleware/services/llm/providers/index.js +1 -1
- package/dist/middleware/services/llm/providers/index.js.map +1 -1
- package/dist/middleware/services/llm/types/anthropic.types.d.ts +79 -0
- package/dist/middleware/services/llm/types/anthropic.types.d.ts.map +1 -0
- package/dist/middleware/services/llm/types/anthropic.types.js +7 -0
- package/dist/middleware/services/llm/types/anthropic.types.js.map +1 -0
- package/dist/middleware/services/llm/types/index.d.ts +1 -0
- package/dist/middleware/services/llm/types/index.d.ts.map +1 -1
- package/dist/middleware/services/llm/types/index.js +1 -0
- package/dist/middleware/services/llm/types/index.js.map +1 -1
- package/package.json +4 -2
package/.env.example
CHANGED
|
@@ -11,6 +11,10 @@ MODEL1_NAME=phi3:mini # Required: Your model name (e.g., phi3:m
|
|
|
11
11
|
MODEL1_URL=http://localhost:11434 # Optional: Defaults to localhost:11434 (Ollama default)
|
|
12
12
|
MODEL1_TOKEN=your_model1_token_here # Optional: For authenticated providers
|
|
13
13
|
|
|
14
|
+
# Anthropic API Configuration (Optional)
|
|
15
|
+
ANTHROPIC_API_KEY=your_anthropic_api_key_here # Your Anthropic API key
|
|
16
|
+
ANTHROPIC_MODEL=claude-3-5-sonnet-20241022 # Default: claude-3-5-sonnet-20241022
|
|
17
|
+
|
|
14
18
|
# Authentication (Optional)
|
|
15
19
|
AUTH_VALIDATION_TYPE=none
|
|
16
20
|
STATIC_API_KEY=your_static_api_key_here
|
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# ๐ LLM Middleware
|
|
4
4
|
|
|
5
|
-
*A comprehensive TypeScript middleware library for building robust multi-provider LLM backends
|
|
5
|
+
*A comprehensive TypeScript middleware library for building robust multi-provider LLM backends. Currently supports Ollama and Anthropic Claude, with OpenAI and Google planned. Features advanced JSON cleaning, logging, error handling, and more.*
|
|
6
6
|
|
|
7
7
|
<!-- Horizontal Badge Navigation Bar -->
|
|
8
8
|
[](https://www.npmjs.com/package/@loonylabs/llm-middleware)
|
|
@@ -40,7 +40,8 @@
|
|
|
40
40
|
- ๐๏ธ **Clean Architecture**: Base classes and interfaces for scalable AI applications
|
|
41
41
|
- ๐ค **Multi-Provider Architecture**: Extensible provider system with strategy pattern
|
|
42
42
|
- โ
**Ollama**: Fully supported with comprehensive parameter control
|
|
43
|
-
-
|
|
43
|
+
- โ
**Anthropic Claude**: Complete support for Claude models (Opus, Sonnet, Haiku)
|
|
44
|
+
- ๐ **OpenAI, Google**: Planned for future releases
|
|
44
45
|
- ๐ **Pluggable**: Easy to add custom providers - see [LLM Providers Guide](docs/LLM_PROVIDERS.md)
|
|
45
46
|
- ๐งน **JSON Cleaning**: Recipe-based JSON repair system with automatic strategy selection
|
|
46
47
|
- ๐จ **FlatFormatter System**: Advanced data formatting for LLM consumption
|
|
@@ -116,7 +117,7 @@ class MyChatUseCase extends BaseAIUseCase<string, MyRequest, MyResult> {
|
|
|
116
117
|
<summary><strong>๐ Using the Multi-Provider Architecture</strong></summary>
|
|
117
118
|
|
|
118
119
|
```typescript
|
|
119
|
-
import { llmService, LLMProvider, ollamaProvider } from '@loonylabs/llm-middleware';
|
|
120
|
+
import { llmService, LLMProvider, ollamaProvider, anthropicProvider } from '@loonylabs/llm-middleware';
|
|
120
121
|
|
|
121
122
|
// Option 1: Use the LLM Service orchestrator (recommended for flexibility)
|
|
122
123
|
const response1 = await llmService.call(
|
|
@@ -128,8 +129,20 @@ const response1 = await llmService.call(
|
|
|
128
129
|
}
|
|
129
130
|
);
|
|
130
131
|
|
|
132
|
+
// Use Anthropic Claude
|
|
133
|
+
const response2 = await llmService.call(
|
|
134
|
+
"Explain quantum computing",
|
|
135
|
+
{
|
|
136
|
+
provider: LLMProvider.ANTHROPIC,
|
|
137
|
+
model: "claude-3-5-sonnet-20241022",
|
|
138
|
+
authToken: process.env.ANTHROPIC_API_KEY,
|
|
139
|
+
maxTokens: 1024,
|
|
140
|
+
temperature: 0.7
|
|
141
|
+
}
|
|
142
|
+
);
|
|
143
|
+
|
|
131
144
|
// Option 2: Use provider directly for provider-specific features
|
|
132
|
-
const
|
|
145
|
+
const response3 = await ollamaProvider.callWithSystemMessage(
|
|
133
146
|
"Write a haiku about coding",
|
|
134
147
|
"You are a creative poet",
|
|
135
148
|
{
|
|
@@ -141,11 +154,21 @@ const response2 = await ollamaProvider.callWithSystemMessage(
|
|
|
141
154
|
}
|
|
142
155
|
);
|
|
143
156
|
|
|
157
|
+
// Or use Anthropic provider directly
|
|
158
|
+
const response4 = await anthropicProvider.call(
|
|
159
|
+
"Write a haiku about coding",
|
|
160
|
+
{
|
|
161
|
+
model: "claude-3-5-sonnet-20241022",
|
|
162
|
+
authToken: process.env.ANTHROPIC_API_KEY,
|
|
163
|
+
maxTokens: 1024
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
|
|
144
167
|
// Set default provider for your application
|
|
145
168
|
llmService.setDefaultProvider(LLMProvider.OLLAMA);
|
|
146
169
|
|
|
147
170
|
// Now calls use Ollama by default
|
|
148
|
-
const
|
|
171
|
+
const response5 = await llmService.call("Hello!", { model: "llama2" });
|
|
149
172
|
```
|
|
150
173
|
|
|
151
174
|
For more details on the multi-provider system, see the [LLM Providers Guide](docs/LLM_PROVIDERS.md).
|
|
@@ -255,13 +278,17 @@ NODE_ENV=development
|
|
|
255
278
|
# Logging
|
|
256
279
|
LOG_LEVEL=info
|
|
257
280
|
|
|
258
|
-
# LLM Provider Configuration
|
|
281
|
+
# LLM Provider Configuration
|
|
259
282
|
MODEL1_NAME=phi3:mini # Required: Your model name
|
|
260
283
|
MODEL1_URL=http://localhost:11434 # Optional: Defaults to localhost (Ollama)
|
|
261
284
|
MODEL1_TOKEN=optional-auth-token # Optional: For authenticated providers
|
|
285
|
+
|
|
286
|
+
# Anthropic API Configuration (Optional)
|
|
287
|
+
ANTHROPIC_API_KEY=your_anthropic_api_key_here # Your Anthropic API key
|
|
288
|
+
ANTHROPIC_MODEL=claude-3-5-sonnet-20241022 # Default Claude model
|
|
262
289
|
```
|
|
263
290
|
|
|
264
|
-
**Multi-Provider Support:**
|
|
291
|
+
**Multi-Provider Support:** The middleware is fully integrated with **Ollama** and **Anthropic Claude**. Support for OpenAI and Google is planned. See the [LLM Providers Guide](docs/LLM_PROVIDERS.md) for details on the provider system and how to use or add providers.
|
|
265
292
|
|
|
266
293
|
</details>
|
|
267
294
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm.service.d.ts","sourceRoot":"","sources":["../../../../src/middleware/services/llm/llm.service.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"llm.service.d.ts","sourceRoot":"","sources":["../../../../src/middleware/services/llm/llm.service.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAGhE,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAE3E,qBAAa,UAAU;IACrB,OAAO,CAAC,SAAS,CAAoC;IACrD,OAAO,CAAC,eAAe,CAAmC;;IAS1D;;OAEG;IACI,WAAW,CAAC,QAAQ,EAAE,WAAW,GAAG,eAAe;IAQ1D;;OAEG;IACI,kBAAkB,CAAC,QAAQ,EAAE,WAAW,GAAG,IAAI;IAOtD;;OAEG;IACI,kBAAkB,IAAI,WAAW;IAIxC;;;OAGG;IACU,qBAAqB,CAChC,UAAU,EAAE,MAAM,EAClB,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,gBAAgB,GAAG;QAAE,QAAQ,CAAC,EAAE,WAAW,CAAA;KAAO,GAC1D,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC;IAMpC;;;OAGG;IACU,IAAI,CACf,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,gBAAgB,GAAG;QAAE,QAAQ,CAAC,EAAE,WAAW,CAAA;KAAO,GAC1D,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC;IAMpC;;OAEG;IACI,qBAAqB,IAAI,WAAW,EAAE;CAG9C;AAGD,eAAO,MAAM,UAAU,YAAmB,CAAC"}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.llmService = exports.LLMService = void 0;
|
|
8
8
|
const ollama_provider_1 = require("./providers/ollama-provider");
|
|
9
|
+
const anthropic_provider_1 = require("./providers/anthropic-provider");
|
|
9
10
|
const types_1 = require("./types");
|
|
10
11
|
class LLMService {
|
|
11
12
|
constructor() {
|
|
@@ -13,6 +14,7 @@ class LLMService {
|
|
|
13
14
|
this.providers = new Map();
|
|
14
15
|
// Initialize available providers
|
|
15
16
|
this.providers.set(types_1.LLMProvider.OLLAMA, new ollama_provider_1.OllamaProvider());
|
|
17
|
+
this.providers.set(types_1.LLMProvider.ANTHROPIC, new anthropic_provider_1.AnthropicProvider());
|
|
16
18
|
}
|
|
17
19
|
/**
|
|
18
20
|
* Get a specific provider instance
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm.service.js","sourceRoot":"","sources":["../../../../src/middleware/services/llm/llm.service.ts"],"names":[],"m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C,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;YAClC,MAAM,IAAI,KAAK,CAAC,YAAY,QAAQ,mBAAmB,CAAC,CAAC;QAC3D,CAAC;QACD,IAAI,CAAC,eAAe,GAAG,QAAQ,CAAC;IAClC,CAAC;IAED;;OAEG;IACI,kBAAkB;QACvB,OAAO,IAAI,CAAC,eAAe,CAAC;IAC9B,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,qBAAqB,CAChC,UAAkB,EAClB,aAAqB,EACrB,UAAyD,EAAE;QAE3D,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,IAAI,CAAC,eAAe,CAAC;QAC1D,MAAM,gBAAgB,GAAG,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;QACpD,OAAO,gBAAgB,CAAC,qBAAqB,CAAC,UAAU,EAAE,aAAa,EAAE,OAAO,CAAC,CAAC;IACpF,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,IAAI,CACf,MAAc,EACd,UAAyD,EAAE;QAE3D,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,IAAI,CAAC,eAAe,CAAC;QAC1D,MAAM,gBAAgB,GAAG,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;QACpD,OAAO,gBAAgB,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChD,CAAC;IAED;;OAEG;IACI,qBAAqB;QAC1B,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC;IAC3C,CAAC;CACF;AAxED,gCAwEC;AAED,4BAA4B;AACf,QAAA,UAAU,GAAG,IAAI,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { BaseLLMProvider } from './base-llm-provider';
|
|
2
|
+
import { CommonLLMResponse } from '../types';
|
|
3
|
+
import { AnthropicRequestOptions } from '../types/anthropic.types';
|
|
4
|
+
/**
|
|
5
|
+
* Anthropic provider implementation with advanced features:
|
|
6
|
+
* - Comprehensive debugging and logging
|
|
7
|
+
* - Error handling with retry logic
|
|
8
|
+
* - Session management
|
|
9
|
+
* - Parameter handling
|
|
10
|
+
*/
|
|
11
|
+
export declare class AnthropicProvider extends BaseLLMProvider {
|
|
12
|
+
private dataFlowLogger;
|
|
13
|
+
private readonly API_VERSION;
|
|
14
|
+
private readonly BASE_URL;
|
|
15
|
+
constructor();
|
|
16
|
+
/**
|
|
17
|
+
* Call the Anthropic API with a custom system message
|
|
18
|
+
* @param userPrompt - The user's prompt for the model
|
|
19
|
+
* @param systemMessage - The system message defining AI behavior
|
|
20
|
+
* @param options - Options for the API call
|
|
21
|
+
* @returns The API response or null on error
|
|
22
|
+
*/
|
|
23
|
+
callWithSystemMessage(userPrompt: string, systemMessage: string, options?: AnthropicRequestOptions): Promise<CommonLLMResponse | null>;
|
|
24
|
+
}
|
|
25
|
+
export declare const anthropicProvider: AnthropicProvider;
|
|
26
|
+
export { AnthropicProvider as AnthropicService };
|
|
27
|
+
export { anthropicProvider as anthropicService };
|
|
28
|
+
//# sourceMappingURL=anthropic-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-provider.d.ts","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/providers/anthropic-provider.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAe,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAC1D,OAAO,EACL,uBAAuB,EAIxB,MAAM,0BAA0B,CAAC;AAIlC;;;;;;GAMG;AACH,qBAAa,iBAAkB,SAAQ,eAAe;IACpD,OAAO,CAAC,cAAc,CAAwB;IAC9C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAgB;IAC5C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAkC;;IAO3D;;;;;;OAMG;IACU,qBAAqB,CAChC,UAAU,EAAE,MAAM,EAClB,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,uBAA4B,GACpC,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC;CAgTrC;AAGD,eAAO,MAAM,iBAAiB,mBAA0B,CAAC;AAGzD,OAAO,EAAE,iBAAiB,IAAI,gBAAgB,EAAE,CAAC;AACjD,OAAO,EAAE,iBAAiB,IAAI,gBAAgB,EAAE,CAAC"}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.anthropicService = exports.AnthropicService = exports.anthropicProvider = exports.AnthropicProvider = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const uuid_1 = require("uuid");
|
|
9
|
+
const logging_utils_1 = require("../../../shared/utils/logging.utils");
|
|
10
|
+
const base_llm_provider_1 = require("./base-llm-provider");
|
|
11
|
+
const types_1 = require("../types");
|
|
12
|
+
const debug_llm_utils_1 = require("../utils/debug-llm.utils");
|
|
13
|
+
const data_flow_logger_1 = require("../../data-flow-logger");
|
|
14
|
+
/**
|
|
15
|
+
* Anthropic provider implementation with advanced features:
|
|
16
|
+
* - Comprehensive debugging and logging
|
|
17
|
+
* - Error handling with retry logic
|
|
18
|
+
* - Session management
|
|
19
|
+
* - Parameter handling
|
|
20
|
+
*/
|
|
21
|
+
class AnthropicProvider extends base_llm_provider_1.BaseLLMProvider {
|
|
22
|
+
constructor() {
|
|
23
|
+
super(types_1.LLMProvider.ANTHROPIC);
|
|
24
|
+
this.API_VERSION = '2023-06-01';
|
|
25
|
+
this.BASE_URL = 'https://api.anthropic.com/v1';
|
|
26
|
+
this.dataFlowLogger = data_flow_logger_1.DataFlowLoggerService.getInstance();
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Call the Anthropic API with a custom system message
|
|
30
|
+
* @param userPrompt - The user's prompt for the model
|
|
31
|
+
* @param systemMessage - The system message defining AI behavior
|
|
32
|
+
* @param options - Options for the API call
|
|
33
|
+
* @returns The API response or null on error
|
|
34
|
+
*/
|
|
35
|
+
async callWithSystemMessage(userPrompt, systemMessage, options = {}) {
|
|
36
|
+
const { authToken = process.env.ANTHROPIC_API_KEY, model = process.env.ANTHROPIC_MODEL || 'claude-3-5-sonnet-20241022', temperature = 0.7, maxTokens = 4096, top_p, top_k, stop_sequences, debugContext, sessionId = (0, uuid_1.v4)(), chapterNumber, pageNumber, pageName } = options;
|
|
37
|
+
// Validate that API key is provided
|
|
38
|
+
if (!authToken) {
|
|
39
|
+
throw new Error('Anthropic API key is required but not provided. ' +
|
|
40
|
+
'Please set ANTHROPIC_API_KEY in your .env file or pass authToken in options.');
|
|
41
|
+
}
|
|
42
|
+
// Validate that model is provided
|
|
43
|
+
if (!model) {
|
|
44
|
+
throw new Error('Model name is required but not provided. ' +
|
|
45
|
+
'Please set ANTHROPIC_MODEL in your .env file or pass model in options.');
|
|
46
|
+
}
|
|
47
|
+
const headers = {
|
|
48
|
+
'Content-Type': 'application/json',
|
|
49
|
+
'x-api-key': authToken,
|
|
50
|
+
'anthropic-version': this.API_VERSION
|
|
51
|
+
};
|
|
52
|
+
// Build the request payload
|
|
53
|
+
const requestPayload = {
|
|
54
|
+
model: model,
|
|
55
|
+
messages: [
|
|
56
|
+
{ role: 'user', content: userPrompt }
|
|
57
|
+
],
|
|
58
|
+
max_tokens: maxTokens,
|
|
59
|
+
temperature: temperature,
|
|
60
|
+
system: systemMessage,
|
|
61
|
+
...(top_p !== undefined && { top_p }),
|
|
62
|
+
...(top_k !== undefined && { top_k }),
|
|
63
|
+
...(stop_sequences && { stop_sequences })
|
|
64
|
+
};
|
|
65
|
+
// Get client request body from global scope
|
|
66
|
+
let clientRequestBody = undefined;
|
|
67
|
+
try {
|
|
68
|
+
clientRequestBody = global.currentRequestBody;
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
// Ignore as it's optional
|
|
72
|
+
}
|
|
73
|
+
// Prepare debug info
|
|
74
|
+
const debugInfo = {
|
|
75
|
+
timestamp: new Date(),
|
|
76
|
+
provider: this.providerName,
|
|
77
|
+
model: model,
|
|
78
|
+
baseUrl: this.BASE_URL,
|
|
79
|
+
systemMessage: systemMessage,
|
|
80
|
+
userMessage: userPrompt,
|
|
81
|
+
requestData: requestPayload,
|
|
82
|
+
useCase: debugContext,
|
|
83
|
+
clientRequestBody: clientRequestBody,
|
|
84
|
+
sessionId: sessionId,
|
|
85
|
+
chapterNumber: chapterNumber,
|
|
86
|
+
pageNumber: pageNumber,
|
|
87
|
+
pageName: pageName
|
|
88
|
+
};
|
|
89
|
+
// Log request
|
|
90
|
+
await debug_llm_utils_1.LLMDebugger.logRequest(debugInfo);
|
|
91
|
+
// Log to data flow logger
|
|
92
|
+
const contextForLogger = {
|
|
93
|
+
currentChapterNr: chapterNumber,
|
|
94
|
+
currentPage: pageNumber,
|
|
95
|
+
debugContext
|
|
96
|
+
};
|
|
97
|
+
const requestId = this.dataFlowLogger.startRequest(debugContext || 'anthropic-direct', contextForLogger);
|
|
98
|
+
this.dataFlowLogger.logLLMRequest({
|
|
99
|
+
stage: debugContext || 'anthropic-direct',
|
|
100
|
+
prompt: userPrompt,
|
|
101
|
+
systemMessage: systemMessage,
|
|
102
|
+
modelName: model,
|
|
103
|
+
temperature: temperature,
|
|
104
|
+
contextInfo: {
|
|
105
|
+
sessionId,
|
|
106
|
+
chapterNumber,
|
|
107
|
+
pageNumber,
|
|
108
|
+
pageName,
|
|
109
|
+
parameters: {
|
|
110
|
+
maxTokens,
|
|
111
|
+
top_p,
|
|
112
|
+
top_k,
|
|
113
|
+
stop_sequences
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}, contextForLogger, requestId);
|
|
117
|
+
const requestStartTime = Date.now();
|
|
118
|
+
try {
|
|
119
|
+
logging_utils_1.logger.info('Sending request to Anthropic API', {
|
|
120
|
+
context: 'AnthropicProvider',
|
|
121
|
+
metadata: {
|
|
122
|
+
url: `${this.BASE_URL}/messages`,
|
|
123
|
+
model: model,
|
|
124
|
+
promptLength: userPrompt.length,
|
|
125
|
+
maxTokens: maxTokens
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
const response = await axios_1.default.post(`${this.BASE_URL}/messages`, requestPayload, {
|
|
129
|
+
headers,
|
|
130
|
+
timeout: 90000 // 90 second timeout
|
|
131
|
+
});
|
|
132
|
+
const requestDuration = Date.now() - requestStartTime;
|
|
133
|
+
if (response && response.status === 200) {
|
|
134
|
+
const apiResponse = response.data;
|
|
135
|
+
// Extract text from content blocks
|
|
136
|
+
const responseText = apiResponse.content
|
|
137
|
+
.map(block => block.text)
|
|
138
|
+
.join('\n');
|
|
139
|
+
// Normalize to CommonLLMResponse format
|
|
140
|
+
const normalizedResponse = {
|
|
141
|
+
message: {
|
|
142
|
+
content: responseText
|
|
143
|
+
},
|
|
144
|
+
sessionId: sessionId,
|
|
145
|
+
metadata: {
|
|
146
|
+
provider: this.providerName,
|
|
147
|
+
model: apiResponse.model,
|
|
148
|
+
tokensUsed: apiResponse.usage.input_tokens + apiResponse.usage.output_tokens,
|
|
149
|
+
processingTime: requestDuration
|
|
150
|
+
},
|
|
151
|
+
// Anthropic-specific fields
|
|
152
|
+
id: apiResponse.id,
|
|
153
|
+
stop_reason: apiResponse.stop_reason || undefined,
|
|
154
|
+
input_tokens: apiResponse.usage.input_tokens,
|
|
155
|
+
output_tokens: apiResponse.usage.output_tokens
|
|
156
|
+
};
|
|
157
|
+
// Add response info to debug
|
|
158
|
+
debugInfo.responseTimestamp = new Date();
|
|
159
|
+
debugInfo.response = responseText;
|
|
160
|
+
debugInfo.rawResponseData = apiResponse;
|
|
161
|
+
// Try to extract thinking content (if model uses <think> tags)
|
|
162
|
+
const thinkMatch = responseText.match(/<think>([\s\S]*?)<\/think>/);
|
|
163
|
+
if (thinkMatch && thinkMatch[1]) {
|
|
164
|
+
debugInfo.thinking = thinkMatch[1].trim();
|
|
165
|
+
}
|
|
166
|
+
// Log response (including markdown saving)
|
|
167
|
+
await debug_llm_utils_1.LLMDebugger.logResponse(debugInfo);
|
|
168
|
+
// Log to data flow logger
|
|
169
|
+
this.dataFlowLogger.logLLMResponse(debugContext || 'anthropic-direct', {
|
|
170
|
+
rawResponse: responseText,
|
|
171
|
+
processingTime: requestDuration
|
|
172
|
+
}, contextForLogger, requestId);
|
|
173
|
+
return normalizedResponse;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
const error = new Error(`Status ${response?.status || 'unknown'}`);
|
|
177
|
+
logging_utils_1.logger.error('Error calling Anthropic API', {
|
|
178
|
+
context: this.constructor.name,
|
|
179
|
+
error: error.message,
|
|
180
|
+
metadata: response?.data || {}
|
|
181
|
+
});
|
|
182
|
+
// Log error to data flow logger
|
|
183
|
+
this.dataFlowLogger.logLLMResponse(debugContext || 'anthropic-direct', {
|
|
184
|
+
rawResponse: '',
|
|
185
|
+
processingTime: Date.now() - requestStartTime,
|
|
186
|
+
error
|
|
187
|
+
}, contextForLogger, requestId);
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
// Type-safe error handling
|
|
193
|
+
let errorMessage = 'Unknown error';
|
|
194
|
+
let errorDetails = {};
|
|
195
|
+
if (error instanceof Error) {
|
|
196
|
+
errorMessage = error.message;
|
|
197
|
+
}
|
|
198
|
+
// Check for Axios error and safely extract properties
|
|
199
|
+
if (error &&
|
|
200
|
+
typeof error === 'object' &&
|
|
201
|
+
'isAxiosError' in error &&
|
|
202
|
+
error.isAxiosError === true) {
|
|
203
|
+
const axiosError = error;
|
|
204
|
+
if (axiosError.response) {
|
|
205
|
+
errorDetails = {
|
|
206
|
+
statusCode: axiosError.response.status,
|
|
207
|
+
statusText: axiosError.response.statusText,
|
|
208
|
+
data: axiosError.response.data
|
|
209
|
+
};
|
|
210
|
+
// Log specific error types
|
|
211
|
+
if (axiosError.response.status === 401) {
|
|
212
|
+
logging_utils_1.logger.error('Authentication error with Anthropic API', {
|
|
213
|
+
context: this.constructor.name,
|
|
214
|
+
error: 'Invalid API key',
|
|
215
|
+
metadata: {
|
|
216
|
+
statusCode: axiosError.response.status,
|
|
217
|
+
message: axiosError.response.data?.error?.message
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
else if (axiosError.response.status === 429) {
|
|
222
|
+
logging_utils_1.logger.error('Rate limit exceeded', {
|
|
223
|
+
context: this.constructor.name,
|
|
224
|
+
error: 'Too many requests',
|
|
225
|
+
metadata: {
|
|
226
|
+
statusCode: axiosError.response.status,
|
|
227
|
+
retryAfter: axiosError.response.headers['retry-after']
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
else if (axiosError.response.status === 400) {
|
|
232
|
+
logging_utils_1.logger.error('Bad request to Anthropic API', {
|
|
233
|
+
context: this.constructor.name,
|
|
234
|
+
error: axiosError.response.data?.error?.message || 'Invalid request',
|
|
235
|
+
metadata: {
|
|
236
|
+
type: axiosError.response.data?.error?.type,
|
|
237
|
+
details: axiosError.response.data?.error
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
logging_utils_1.logger.error('Error in API request', {
|
|
244
|
+
context: this.constructor.name,
|
|
245
|
+
error: errorMessage,
|
|
246
|
+
metadata: {
|
|
247
|
+
...errorDetails,
|
|
248
|
+
requestModel: model,
|
|
249
|
+
sessionId: sessionId
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
// Log error to data flow logger
|
|
253
|
+
this.dataFlowLogger.logLLMResponse(debugContext || 'anthropic-direct', {
|
|
254
|
+
rawResponse: '',
|
|
255
|
+
processingTime: Date.now() - requestStartTime,
|
|
256
|
+
error: error instanceof Error ? error : new Error(errorMessage)
|
|
257
|
+
}, contextForLogger, requestId);
|
|
258
|
+
// Add error info to debug
|
|
259
|
+
debugInfo.responseTimestamp = new Date();
|
|
260
|
+
debugInfo.error = {
|
|
261
|
+
message: errorMessage,
|
|
262
|
+
details: errorDetails
|
|
263
|
+
};
|
|
264
|
+
// Log error
|
|
265
|
+
await debug_llm_utils_1.LLMDebugger.logError(debugInfo);
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
exports.AnthropicProvider = AnthropicProvider;
|
|
271
|
+
exports.AnthropicService = AnthropicProvider;
|
|
272
|
+
// Export singleton instance
|
|
273
|
+
exports.anthropicProvider = new AnthropicProvider();
|
|
274
|
+
exports.anthropicService = exports.anthropicProvider;
|
|
275
|
+
//# sourceMappingURL=anthropic-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-provider.js","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/providers/anthropic-provider.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA0B;AAC1B,+BAAoC;AACpC,uEAA6D;AAC7D,2DAAsD;AACtD,oCAA0D;AAO1D,8DAAqE;AACrE,6DAA+D;AAE/D;;;;;;GAMG;AACH,MAAa,iBAAkB,SAAQ,mCAAe;IAKpD;QACE,KAAK,CAAC,mBAAW,CAAC,SAAS,CAAC,CAAC;QAJd,gBAAW,GAAG,YAAY,CAAC;QAC3B,aAAQ,GAAG,8BAA8B,CAAC;QAIzD,IAAI,CAAC,cAAc,GAAG,wCAAqB,CAAC,WAAW,EAAE,CAAC;IAC5D,CAAC;IAED;;;;;;OAMG;IACI,KAAK,CAAC,qBAAqB,CAChC,UAAkB,EAClB,aAAqB,EACrB,UAAmC,EAAE;QAErC,MAAM,EACJ,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,EACzC,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,4BAA4B,EACnE,WA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GAAG,IAAI,IAAI,EAAE,CAAC;YACzC,SAAS,CAAC,KAAK,GAAG;gBAChB,OAAO,EAAE,YAAY;gBACrB,OAAO,EAAE,YAAY;aACtB,CAAC;YAEF,YAAY;YACZ,MAAM,6BAAW,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;YAEtC,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;CACF;AArUD,8CAqUC;AAM6B,6CAAgB;AAJ9C,4BAA4B;AACf,QAAA,iBAAiB,GAAG,IAAI,iBAAiB,EAAE,CAAC;AAI3B,2BAJjB,yBAAiB,CAIgB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/providers/index.ts"],"names":[],"mappings":"AACA,cAAc,qBAAqB,CAAC;AAGpC,cAAc,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/providers/index.ts"],"names":[],"mappings":"AACA,cAAc,qBAAqB,CAAC;AAGpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC"}
|
|
@@ -18,8 +18,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
18
18
|
__exportStar(require("./base-llm-provider"), exports);
|
|
19
19
|
// Concrete providers
|
|
20
20
|
__exportStar(require("./ollama-provider"), exports);
|
|
21
|
+
__exportStar(require("./anthropic-provider"), exports);
|
|
21
22
|
// Future providers will be added here:
|
|
22
23
|
// export * from './openai-provider';
|
|
23
|
-
// export * from './anthropic-provider';
|
|
24
24
|
// export * from './google-provider';
|
|
25
25
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/providers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gBAAgB;AAChB,sDAAoC;AAEpC,qBAAqB;AACrB,oDAAkC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/providers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gBAAgB;AAChB,sDAAoC;AAEpC,qBAAqB;AACrB,oDAAkC;AAClC,uDAAqC;AAErC,uCAAuC;AACvC,qCAAqC;AACrC,qCAAqC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic-specific types and interfaces
|
|
3
|
+
* Based on Anthropic Messages API: https://docs.anthropic.com/en/api/messages
|
|
4
|
+
*/
|
|
5
|
+
import { CommonLLMOptions, CommonLLMResponse } from './common.types';
|
|
6
|
+
/**
|
|
7
|
+
* Anthropic-specific request options
|
|
8
|
+
* Extends common options with Anthropic-specific parameters
|
|
9
|
+
*/
|
|
10
|
+
export interface AnthropicRequestOptions extends CommonLLMOptions {
|
|
11
|
+
/** Top-p sampling (nucleus sampling) - Range: 0.0 to 1.0 */
|
|
12
|
+
top_p?: number;
|
|
13
|
+
/** Top-k sampling - Only sample from top K options */
|
|
14
|
+
top_k?: number;
|
|
15
|
+
/** Custom text sequences that will cause the model to stop generating */
|
|
16
|
+
stop_sequences?: string[];
|
|
17
|
+
/** Whether to stream the response */
|
|
18
|
+
stream?: boolean;
|
|
19
|
+
/** System message to set context (Anthropic puts this at root level) */
|
|
20
|
+
system?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Anthropic message format
|
|
24
|
+
*/
|
|
25
|
+
export interface AnthropicMessage {
|
|
26
|
+
role: 'user' | 'assistant';
|
|
27
|
+
content: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Anthropic API request payload
|
|
31
|
+
*/
|
|
32
|
+
export interface AnthropicAPIRequest {
|
|
33
|
+
model: string;
|
|
34
|
+
messages: AnthropicMessage[];
|
|
35
|
+
max_tokens: number;
|
|
36
|
+
temperature?: number;
|
|
37
|
+
top_p?: number;
|
|
38
|
+
top_k?: number;
|
|
39
|
+
stop_sequences?: string[];
|
|
40
|
+
stream?: boolean;
|
|
41
|
+
system?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Anthropic content block (response can have multiple blocks)
|
|
45
|
+
*/
|
|
46
|
+
export interface AnthropicContentBlock {
|
|
47
|
+
type: 'text';
|
|
48
|
+
text: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Anthropic usage information
|
|
52
|
+
*/
|
|
53
|
+
export interface AnthropicUsage {
|
|
54
|
+
input_tokens: number;
|
|
55
|
+
output_tokens: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Anthropic API response format
|
|
59
|
+
*/
|
|
60
|
+
export interface AnthropicAPIResponse {
|
|
61
|
+
id: string;
|
|
62
|
+
type: 'message';
|
|
63
|
+
role: 'assistant';
|
|
64
|
+
content: AnthropicContentBlock[];
|
|
65
|
+
model: string;
|
|
66
|
+
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | null;
|
|
67
|
+
stop_sequence?: string | null;
|
|
68
|
+
usage: AnthropicUsage;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Anthropic-specific response format (normalized to CommonLLMResponse)
|
|
72
|
+
*/
|
|
73
|
+
export interface AnthropicResponse extends CommonLLMResponse {
|
|
74
|
+
id?: string;
|
|
75
|
+
stop_reason?: string;
|
|
76
|
+
input_tokens?: number;
|
|
77
|
+
output_tokens?: number;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=anthropic.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic.types.d.ts","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/types/anthropic.types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAErE;;;GAGG;AACH,MAAM,WAAW,uBAAwB,SAAQ,gBAAgB;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,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,KAAK,EAAE,cAAc,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAkB,SAAQ,iBAAiB;IAE1D,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic.types.js","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/types/anthropic.types.ts"],"names":[],"mappings":";AAAA;;;GAGG"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/types/index.ts"],"names":[],"mappings":"AACA,cAAc,gBAAgB,CAAC;AAG/B,cAAc,gBAAgB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/types/index.ts"],"names":[],"mappings":"AACA,cAAc,gBAAgB,CAAC;AAG/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC"}
|
|
@@ -18,4 +18,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
18
18
|
__exportStar(require("./common.types"), exports);
|
|
19
19
|
// Provider-specific types
|
|
20
20
|
__exportStar(require("./ollama.types"), exports);
|
|
21
|
+
__exportStar(require("./anthropic.types"), exports);
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,eAAe;AACf,iDAA+B;AAE/B,0BAA0B;AAC1B,iDAA+B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/middleware/services/llm/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,eAAe;AACf,iDAA+B;AAE/B,0BAA0B;AAC1B,iDAA+B;AAC/B,oDAAkC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loonylabs/llm-middleware",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "Complete middleware infrastructure for LLM-based backends with multi-provider support (Ollama,
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "Complete middleware infrastructure for LLM-based backends with multi-provider support (Ollama, Anthropic, OpenAI, Google)",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
@@ -22,6 +22,8 @@
|
|
|
22
22
|
"test:manual:verify-params": "node tests/manual/verify-parameters.js",
|
|
23
23
|
"test:manual:formatter-demo": "ts-node tests/manual/request-formatter-demo.ts",
|
|
24
24
|
"test:manual:story-test": "ts-node tests/manual/story-generator-test.ts",
|
|
25
|
+
"test:provider:ollama": "TEST_PROVIDER=ollama ts-node tests/manual/provider-smoke-test.ts",
|
|
26
|
+
"test:provider:anthropic": "TEST_PROVIDER=anthropic ts-node tests/manual/provider-smoke-test.ts",
|
|
25
27
|
"test:ci": "jest --runInBand --ci --coverage",
|
|
26
28
|
"lint": "eslint src/**/*.ts",
|
|
27
29
|
"clean": "rm -rf dist",
|