Skip to content

Commit

Permalink
refactored code for publishing NPM package
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-kural committed Jul 7, 2024
1 parent fb2b131 commit cd70e5e
Show file tree
Hide file tree
Showing 35 changed files with 401 additions and 361 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,5 @@ dist
*.pem

# Test files
test.txt
test.txt
tmp-test-flows.ts
11 changes: 4 additions & 7 deletions .swcrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"minify": true,
"minify": false,
"jsc": {
"parser": {
"syntax": "typescript",
Expand All @@ -9,11 +9,7 @@
},
"target": "es2017",
"loose": false,
"transform": {
"legacyDecorator": false,
"decoratorVersion": "2022-03"
},
"keepClassNames": false,
"keepClassNames": true,
"baseUrl": "./src",
"paths": {
"/*": ["./*"]
Expand All @@ -23,6 +19,7 @@
"isModule": true,
"module": {
"type": "commonjs",
"strictMode": true
"strictMode": true,
"noInterop": true
}
}
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ For failed requests, the response will contain:
Check `src/index.ts` to see how you can define an open-ended chat flow.
```typescript
import { defineChatFlow } from "./flows/flow";
import { defineChatEndpoint } from "./flows/flow";
// Open-ended chat flow
defineChatFlow({
defineChatEndpoint({
chatAgent: new ChatAgent(),
endpoint: ENDPOINTS.CHAT.OPEN_ENDED,
});
Expand Down Expand Up @@ -239,10 +239,10 @@ For failed requests, the response will contain:
Check `src/index.ts` to see how you can define an open-ended chat flow that supports chat history.

```typescript
import { defineChatFlow } from "./flows/flow";
import { defineChatEndpoint } from "./flows/flow";
// Open-ended chat flow with support for chat history
defineChatFlow({
defineChatEndpoint({
chatAgent: new ChatAgent({
useChatHistory: true,
}),
Expand Down Expand Up @@ -318,10 +318,10 @@ For failed requests, the response will contain:
Check `src/index.ts` to see how you can define an open-ended chat flow that supports chat history, response caching, and API key authentication.
```typescript
import { defineChatFlow } from "./flows/flow";
import { defineChatEndpoint } from "./flows/flow";
// Open-ended chat flow with support for chat history, authentication, and caching
defineChatFlow({
defineChatEndpoint({
chatAgent: new ChatAgent({
useChatHistory: true,
}),
Expand Down Expand Up @@ -386,10 +386,10 @@ For failed requests, the response will contain:
Check `src/index.ts` to see how you can define a close-ended chat flow.
```typescript
import { defineChatFlow } from "./flows/flow";
import { defineChatEndpoint } from "./flows/flow";
// Close-ended chat flow (will only answer queries related to specified topic, in this case, 'Firebase')
defineChatFlow(
defineChatEndpoint(
new ChatAgent({
agentTypeConfig: {
agentType: "close-ended",
Expand Down Expand Up @@ -461,10 +461,10 @@ For failed requests, the response will contain:
Check `src/index.ts` to see how you can define a close-ended chat flow that supports chat history.
```typescript
import { defineChatFlow } from "./flows/flow";
import { defineChatEndpoint } from "./flows/flow";
// Close-ended chat flow with support for chat history
defineChatFlow({
defineChatEndpoint({
chatAgent: new ChatAgent({
agentTypeConfig: {
agentType: "close-ended",
Expand Down Expand Up @@ -540,10 +540,10 @@ For failed requests, the response will contain:
Check `src/index.ts` to see how you can define a close-ended chat flow that supports chat history, response caching, and API key authentication.
```typescript
import { defineChatFlow } from "./flows/flow";
import { defineChatEndpoint } from "./flows/flow";
// Close-ended chat flow with support for chat history, authentication, and caching
defineChatFlow({
defineChatEndpoint({
chatAgent: new ChatAgent({
agentTypeConfig: {
agentType: "close-ended",
Expand Down Expand Up @@ -630,13 +630,13 @@ Check `src/index.ts` to see how you can define a RAG chat flow that supports cha
Below example uses a RAG agent to answer user queries related to 'Store Inventory Data'. The sample inventory data is stored in `rag/knowledge-bases/test-retail-store-kb/inventory-data.csv` in CSV format. We'll index this data, store it in a vector store, and use a vector store retriever to retrieve the data when answering user queries.
```typescript
import { defineChatFlow } from "./flows/flow";
import { defineChatEndpoint } from "./flows/flow";
// Index inventory data and get retriever
const inventoryDataRetriever = await getInventoryDataRetriever();
// Inventory Data chat flow with support for chat history, authentication, caching and RAG
defineChatFlow({
defineChatEndpoint({
chatAgent: new ChatAgent({
agentTypeConfig: {
agentType: "rag",
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { InMemoryChatHistoryStore } from "./lib/history/chat-history-store";

new InMemoryChatHistoryStore();
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const config: Config = {
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
testMatch: ["**/lib/tests/**/*.[jt]s?(x)"],
testMatch: ["**/src/tests/**/*.[jt]s?(x)"],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
Expand Down
111 changes: 108 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"scripts": {
"start": "nodemon -x tsx src/index.js",
"dev": "npm run build && npm run start",
"build": "swc --strip-leading-paths --delete-dir-on-start -d lib src && node scripts/copy-build-files.mjs",
"build": "swc --strip-leading-paths --delete-dir-on-start -d lib src && tsc",
"buildtypes": "tsc",
"lint": "pnpm eslint .",
"format": "pnpm prettier . --write",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --no-watchman",
Expand All @@ -20,9 +21,113 @@
"langchain",
"genkit"
],
"author": "Oconva <oconva.org@gmail.com> (https://github.com/oconva)",
"license": "ISC",
"author": "Pranav Kural <oconva-kural@gmail.com> (https://github.com/pranav-kural)",
"license": "MIT",
"description": "Framework to build secure, performant and reliable chat apps and services quickly and efficiently.",
"files": [
"lib",
"src",
"*"
],
"types": "lib/index.d.ts",
"exports": {
"./agents": {
"types": "./lib/agents/chat-agents.d.ts",
"import": "./lib/agents/chat-agent.js",
"require": "./lib/agents/chat-agent.js",
"default": "./lib/agents/chat-agent.js"
},
"./auth": {
"types": "./lib/auth/api-key-store.d.ts",
"import": "./lib/auth/api-key-store.js",
"require": "./lib/auth/api-key-store.js",
"default": "./lib/auth/api-key-store.js"
},
"./cache": {
"types": "./lib/cache/cache-store.d.ts",
"import": "./lib/cache/cache-store.js",
"require": "./lib/cache/cache-store.js",
"default": "./lib/cache/cache-store.js"
},
"./config": {
"types": "./lib/config/config.d.ts",
"import": "./lib/config/config.js",
"require": "./lib/config/config.js",
"default": "./lib/config/config.js"
},
"./endpoints": {
"types": "./lib/endpoints/endpoints.d.ts",
"import": "./lib/endpoints/endpoints.js",
"require": "./lib/endpoints/endpoints.js",
"default": "./lib/endpoints/endpoints.js"
},
"./genkit": {
"types": "./lib/genkit/genkit.d.ts",
"import": "./lib/genkit/genkit.js",
"require": "./lib/genkit/genkit.js",
"default": "./lib/genkit/genkit.js"
},
"./history": {
"types": "./lib/history/chat-history-store.d.ts",
"import": "./lib/history/chat-history-store.js",
"require": "./lib/history/chat-history-store.js",
"default": "./lib/history/chat-history-store.js"
},
"./firebase": {
"types": "./lib/integrations/firebase.d.ts",
"import": "./lib/integrations/firebase.js",
"require": "./lib/integrations/firebase.js",
"default": "./lib/integrations/firebase.js"
},
"./models": {
"types": "./lib/models/models.d.ts",
"import": "./lib/models/models.js",
"require": "./lib/models/models.js",
"default": "./lib/models/models.js"
},
"./prompts": {
"types": "./lib/prompts/prompts.d.ts",
"import": "./lib/prompts/prompts.js",
"require": "./lib/prompts/prompts.js",
"default": "./lib/prompts/prompts.js"
},
"./rag": {
"types": "./lib/rag/rag.d.ts",
"import": "./lib/rag/rag.js",
"require": "./lib/rag/rag.js",
"default": "./lib/rag/rag.js"
},
"./data-embeddings": {
"types": "./lib/rag/data-embeddings/embedding-models.d.ts",
"import": "./lib/rag/data-embeddings/embedding-models.js",
"require": "./lib/rag/data-embeddings/embedding-models.js",
"default": "./lib/rag/data-embeddings/embedding-models.js"
},
"./data-loaders": {
"types": "./lib/rag/data-loaders/data-loaders.d.ts",
"import": "./lib/rag/data-loaders/data-loaders.js",
"require": "./lib/rag/data-loaders/data-loaders.js",
"default": "./lib/rag/data-loaders/data-loaders.js"
},
"./data-retrievers": {
"types": "./lib/rag/data-retrievers/data-retrievers.d.ts",
"import": "./lib/rag/data-retrievers/data-retrievers.js",
"require": "./lib/rag/data-retrievers/data-retrievers.js",
"default": "./lib/rag/data-retrievers/data-retrievers.js"
},
"./data-splitters": {
"types": "./lib/rag/data-splitters/data-splitters.d.ts",
"import": "./lib/rag/data-splitters/data-splitters.js",
"require": "./lib/rag/data-splitters/data-splitters.js",
"default": "./lib/rag/data-splitters/data-splitters.js"
},
"./utils": {
"types": "./lib/utils/utils.d.ts",
"import": "./lib/utils/utils.js",
"require": "./lib/utils/utils.js",
"default": "./lib/utils/utils.js"
}
},
"devDependencies": {
"@eslint/js": "^9.6.0",
"@swc/cli": "^0.4.0",
Expand Down
21 changes: 0 additions & 21 deletions scripts/copy-build-files.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion src/agents/chat-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ModelConfig,
SupportedModelNames,
SupportedModels,
} from "../models/model";
} from "../models/models";
import {
closeEndedSystemPrompt,
openEndedSystemPrompt,
Expand Down
8 changes: 8 additions & 0 deletions src/auth/api-key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,11 @@ export interface APIKeyStore {
*/
incrementRequests: (key: APIKey) => Promise<boolean> | Promise<WriteResult>;
}

// Export supported API key stores
export { InMemoryAPIKeyStore } from "./in-memory-api-key-store";

export {
FirestoreAPIKeyStore,
type FirestoreAPIKeyStoreConfig,
} from "./firestore-api-key-store";
8 changes: 4 additions & 4 deletions src/auth/firestore-api-key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { app } from "firebase-admin";
* Configurations for the FirestoreAPIKeyStore.
* @property collectionName - The name of the Firestore collection to store API keys.
*/
type FirestoreAPIKeyStoreConfig = {
export type FirestoreAPIKeyStoreConfig = {
firebaseApp: app.App;
collectionName: string;
};
Expand Down Expand Up @@ -70,18 +70,18 @@ export class FirestoreAPIKeyStore implements APIKeyStore {
async updateKey({
key,
status,
flows,
endpoints,
requests,
}: {
key: string;
status?: APIKeyStatus;
flows?: string[] | "all";
endpoints?: string[] | "all";
requests?: number;
}): Promise<WriteResult> {
// values to update
const valuesToUpdate = {
...(status && { status }),
...(flows && { endpoints: flows }),
...(endpoints && { endpoints: endpoints }),
...(requests && { requests }),
};
// Update the API key in the Firestore collection
Expand Down
8 changes: 4 additions & 4 deletions src/auth/in-memory-api-key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ export class InMemoryAPIKeyStore implements APIKeyStore {
async updateKey({
key,
status,
flows,
endpoints,
requests,
}: {
key: string;
status?: APIKeyStatus;
flows?: string[] | "all";
endpoints?: string[] | "all";
requests?: number;
}): Promise<boolean> {
const apiKey = this.keys.get(key);
Expand All @@ -68,8 +68,8 @@ export class InMemoryAPIKeyStore implements APIKeyStore {
if (status) {
apiKey.status = status;
}
if (flows) {
apiKey.endpoints = flows;
if (endpoints) {
apiKey.endpoints = endpoints;
}
if (requests) {
apiKey.requests = requests;
Expand Down
7 changes: 7 additions & 0 deletions src/cache/cache-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,10 @@ export interface CacheStore {
*/
incrementCacheHits(hash: QueryHash): Promise<void> | Promise<WriteResult>;
}

// export supported cache stores
export { InMemoryCacheStore } from "./in-memory-cache-store";
export {
FirestoreCacheStore,
type FirestoreCacheStoreConfig,
} from "./firestore-cache-store";
Loading

0 comments on commit cd70e5e

Please sign in to comment.