Skip to content

Commit 67da79a

Browse files
committed
📦 NEW: Config based env
1 parent 3e3dfea commit 67da79a

File tree

16 files changed

+25168
-18956
lines changed

16 files changed

+25168
-18956
lines changed

examples/nodejs/baseai/baseai.config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import type {BaseAIConfig} from 'baseai';
2-
3-
export const config: BaseAIConfig = {
1+
export const config = {
42
log: {
53
isEnabled: true,
64
logSensitiveData: false,
@@ -15,4 +13,8 @@ export const config: BaseAIConfig = {
1513
useLocalEmbeddings: false,
1614
},
1715
envFilePath: '.env',
16+
env: {
17+
langbase: '===========================================================',
18+
openai: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
19+
},
1820
};

examples/nodejs/baseai/pipes/summary.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {PipeI} from '@baseai/core';
2+
import {config} from '../baseai.config';
23

34
const buildPipe = (): PipeI => ({
4-
apiKey: process.env.LANGBASE_API_KEY!, // Replace with your API key https://langbase.com/docs/api-reference/api-keys
5-
name: 'summary',
5+
apiKey: config.env.langbase,
6+
name: 'summary-nodejs',
67
description: '',
78
status: 'private',
89
model: 'openai:gpt-4o-mini',
@@ -18,7 +19,12 @@ const buildPipe = (): PipeI => ({
1819
stop: [],
1920
tool_choice: 'auto',
2021
parallel_tool_calls: false,
21-
messages: [{role: 'system', content: `You are a helpful AI assistant.`}],
22+
messages: [
23+
{
24+
role: 'system',
25+
content: `You are a helpful AI assistant. Make everything less wordy.`,
26+
},
27+
],
2228
variables: [],
2329
memory: [],
2430
tools: [],

examples/nodejs/examples/pipe.run.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import 'dotenv/config';
2-
import {Pipe} from '@baseai/core';
2+
import {config} from '../baseai/baseai.config';
33
import pipeSummary from '../baseai/pipes/summary';
4+
import {Pipe} from './../../../packages/core/src/pipes/pipes';
45

5-
const pipe = new Pipe(pipeSummary());
6+
const pipe = new Pipe({
7+
...pipeSummary(),
8+
config,
9+
});
610

711
async function main() {
812
const userMsg = 'Who is an AI Engineer?';

packages/baseai/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
},
2525
"homepage": "https://BaseAI.dev",
2626
"files": [
27-
"dist/**"
27+
"dist/**",
28+
"bin/**"
2829
],
2930
"scripts": {
3031
"build": "tsup",

packages/baseai/src/add/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,9 @@ async function createLocalPipe(pipe: Pipe) {
302302
${toolData.map(tool => tool.importPath).join('\n')}
303303
304304
const ${pipeNameCamelCase} = (): PipeI => ({
305-
// Replace with your API key https://langbase.com/docs/api-reference/api-keys
306-
apiKey: process.env.LANGBASE_API_KEY!,
305+
// Prod only: Replace with your Langbase API key
306+
// https://langbase.com/docs/api-reference/api-keys
307+
apiKey: process.env.LANGBASE_API_KEY,
307308
name: \`${pipe.name}\`,
308309
description: \`${pipe.description}\`,
309310
status: \`${pipe.status}\`,

packages/baseai/src/dev/utils/get-llm-api-key.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

packages/baseai/src/pipe/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ export async function createPipe() {
132132
const pipeContent = `import { PipeI } from '@baseai/core';${selectedTools}${selectedMemories}
133133
134134
const ${pipeNameCamelCase} = (): PipeI => ({
135-
// Replace with your API key https://langbase.com/docs/api-reference/api-keys
136-
apiKey: process.env.LANGBASE_API_KEY!,
135+
// Prod only: Replace with your Langbase API key
136+
// https://langbase.com/docs/api-reference/api-keys
137+
apiKey: process.env.LANGBASE_API_KEY,
137138
name: '${pipeNameSlugified}',
138139
description: '${pipeInfo.description || ''}',
139140
status: '${pipeInfo.status}',

packages/baseai/src/utils/memory/generate-openai-embeddings.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
import * as p from '@clack/prompts';
22
import { getEncoding } from 'js-tiktoken';
33
import OpenAI from 'openai';
4+
import { loadConfig } from '../config/config-handler';
5+
import { cyan } from '../formatting';
46
import { MEMORYSETS } from './constants';
57

68
export const getOpenAIEmbeddings = async (
79
chunks: string[]
810
): Promise<OpenAI.Embeddings.Embedding[]> => {
9-
const openAiKey = process.env.OPENAI_API_KEY;
11+
const config = await loadConfig();
12+
const configEnv = config?.env;
13+
14+
const getEnv = (key: string): string | undefined => {
15+
let value: string | undefined;
16+
if (configEnv && key in configEnv) {
17+
value = configEnv[key as keyof typeof configEnv];
18+
} else {
19+
value = process.env[key];
20+
}
21+
22+
return value;
23+
};
24+
25+
const openAiKey = getEnv('OPENAI_API_KEY');
1026

1127
if (!openAiKey) {
1228
p.cancel(
13-
'OpenAI key not found. Please set the OPENAI_API_KEY environment variable. Only required locally, in production, add it to your keysets https://langbase.com/docs/features/keysets'
29+
`Environment variable ${cyan(`OPENAI_API_KEY`)} is not set or empty. Only needed in local dev environment. \nNote: In production, add it to your keysets https://langbase.com/docs/features/keysets\n`
1430
);
1531
process.exit(1);
1632
}

packages/baseai/types/config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,16 @@ export interface BaseAIConfig {
2929
log: LoggerConfig;
3030
memory: MemoryConfig;
3131
envFilePath: string;
32+
env: {
33+
NODE_ENV: string;
34+
OPENAI_API_KEY: string;
35+
ANTHROPIC_API_KEY: string;
36+
COHERE_API_KEY: string;
37+
FIREWORKS_API_KEY: string;
38+
GOOGLE_API_KEY: string;
39+
GROQ_API_KEY: string;
40+
MISTRAL_API_KEY: string;
41+
PERPLEXITY_API_KEY: string;
42+
TOGETHER_API_KEY: string;
43+
};
3244
}

packages/core/src/common/request.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Stream} from 'openai/streaming';
2+
import {Logger} from 'src/helpers/logger';
23
import {APIConnectionError, APIError} from './errors';
34

45
interface RequestOptions {
@@ -10,10 +11,11 @@ interface RequestOptions {
1011
rawResponse?: boolean;
1112
}
1213

13-
interface RequestConfig {
14+
interface RequestProps {
1415
apiKey?: string;
1516
baseUrl: string;
1617
timeout?: number;
18+
config?: any; // TODO: BaseAIConfig
1719
}
1820

1921
interface SendOptions extends RequestOptions {
@@ -34,10 +36,12 @@ interface HandleGenerateResponseParams {
3436
}
3537

3638
export class Request {
37-
private config: RequestConfig;
39+
private console: any;
40+
private props: RequestProps;
3841

39-
constructor(config: RequestConfig) {
40-
this.config = config;
42+
constructor(props: RequestProps) {
43+
this.props = props;
44+
this.console = new Logger(this.props.config);
4145
}
4246

4347
private async send<T>({endpoint, ...options}: SendOptions): Promise<T> {
@@ -79,7 +83,7 @@ export class Request {
7983
}
8084

8185
private buildUrl({endpoint}: {endpoint: string}): string {
82-
return `${this.config.baseUrl}${endpoint}`;
86+
return `${this.props.baseUrl}${endpoint}`;
8387
}
8488

8589
private buildHeaders({
@@ -89,7 +93,7 @@ export class Request {
8993
}): Record<string, string> {
9094
return {
9195
'Content-Type': 'application/json',
92-
Authorization: `Bearer ${this.config.apiKey}`,
96+
Authorization: `Bearer ${this.props.apiKey}`,
9397
...headers,
9498
};
9599
}
@@ -99,12 +103,17 @@ export class Request {
99103
options,
100104
headers,
101105
}: MakeRequestParams): Promise<Response> {
102-
// console.log(' =================== REQUEST ===================');
106+
this.console.log('pipe.request', {
107+
url,
108+
method: options.method,
109+
headers,
110+
body: options.body,
111+
});
103112
const resp = await fetch(url, {
104113
method: options.method,
105114
headers,
106115
body: JSON.stringify(options.body),
107-
signal: AbortSignal.timeout(this.config.timeout || 30000),
116+
signal: AbortSignal.timeout(this.props.timeout || 30000),
108117
});
109118
return resp;
110119
}
@@ -194,9 +203,6 @@ export class Request {
194203
}
195204

196205
async post<T>(options: Omit<RequestOptions, 'method'>): Promise<T> {
197-
console.log('Request.post.options');
198-
console.dir(options, {depth: null, colors: true});
199-
200206
return this.send<T>({...options, method: 'POST'});
201207
}
202208

0 commit comments

Comments
 (0)