Skip to content

Commit

Permalink
adding-react-chain-example
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Raghuwanshi committed May 13, 2024
1 parent dbd06e6 commit b0f3e34
Show file tree
Hide file tree
Showing 31 changed files with 450 additions and 654 deletions.
Binary file removed JS/edgechains/arakoodev/arakoodev-1.0.7.tgz
Binary file not shown.
1 change: 0 additions & 1 deletion JS/edgechains/arakoodev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"files": [
"dist"
],
"type": "module",
"exports": {
"./openai": "./dist/openai/src/index.js",
"./vector-db": "./dist/vector-db/src/index.js",
Expand Down
Binary file not shown.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface ChatOpenAiOptions {
orgId?: string;
model?: string;
role?: string;
max_tokens: number;
temperature?: number;
}

Expand All @@ -18,11 +19,13 @@ export class ChatOpenAi {
orgId: string;
model: string;
role: string;
max_tokens: number;
temperature: number;

constructor(options: ChatOpenAiOptions = {}) {
constructor(options: ChatOpenAiOptions) {
this.url = options.url || openAI_url;
this.openAIApiKey = options.openAIApiKey || process.env.OPENAI_API_KEY!; // and check it's there
this.max_tokens = options.max_tokens || 256;
this.openAIApiKey = options.openAIApiKey || process.env.OPENAI_API_KEY!;
this.orgId = options.orgId || "";
this.model = options.model || "gpt-3.5-turbo";
this.role = options.role || "user";
Expand All @@ -41,21 +44,25 @@ export class ChatOpenAi {
content: prompt,
},
],
max_tokens: this.max_tokens,
temperature: this.temperature,
},
{
headers: {
Authorization: "Bearer " + this.openAIApiKey,
"content-type": "application/json",
},
}
},
)
.then((response) => {
return response.data.choices;
})
.catch((error) => {
if (error.response) {
console.log("Server responded with status code:", error.response.status);
console.log(
"Server responded with status code:",
error.response.status,
);
console.log("Response data:", error.response.data);
} else if (error.request) {
console.log("No response received:", error);
Expand All @@ -79,14 +86,17 @@ export class ChatOpenAi {
Authorization: `Bearer ${this.openAIApiKey}`,
"content-type": "application/json",
},
}
},
)
.then((response) => {
return response.data.data;
})
.catch((error) => {
if (error.response) {
console.log("Server responded with status code:", error.response.status);
console.log(
"Server responded with status code:",
error.response.status,
);
console.log("Response data:", error.response.data);
} else if (error.request) {
console.log("No response received:", error.request);
Expand All @@ -111,15 +121,18 @@ export class ChatOpenAi {
Authorization: "Bearer " + this.openAIApiKey,
"content-type": "application/json",
},
}
},
)
.then((response) => {
return response.data.choices;
})
.catch((error) => {
console.log({ error });
if (error.response) {
console.log("Server responded with status code:", error.response.status);
console.log(
"Server responded with status code:",
error.response.status,
);
console.log("Response data:", error.response.data);
} else if (error.request) {
console.log("No response received:", error.request);
Expand Down Expand Up @@ -150,14 +163,17 @@ export class ChatOpenAi {
Authorization: "Bearer " + this.openAIApiKey,
"content-type": "application/json",
},
}
},
)
.then(function (response) {
return response.data.choices;
})
.catch(function (error) {
if (error.response) {
console.log("Server responded with status code:", error.response.status);
console.log(
"Server responded with status code:",
error.response.status,
);
console.log("Response data:", error.response.data);
} else if (error.request) {
console.log("No response received:", error.request);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ts-ignore
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";

export interface OpenAIStreamPayload {
Expand Down
3 changes: 0 additions & 3 deletions JS/edgechains/examples/chat-with-pdf/.babelrc

This file was deleted.

17 changes: 3 additions & 14 deletions JS/edgechains/examples/chat-with-pdf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
"main": "index.js",
"type": "module",
"scripts": {
"test": "npx jest",
"test": "npx vitest",
"build": "tsc -b",
"start": "node --experimental-wasm-modules dist/index.js"
},
"jest": {
"setupFiles": [
"<rootDir>/setupTests.js"
]
"start": "tsc && node --experimental-wasm-modules dist/index.js"
},
"keywords": [],
"author": "",
Expand All @@ -21,17 +16,11 @@
"@arakoodev/jsonnet": "^0.2.0",
"@babel/preset-env": "^7.24.4",
"arakoodev": "^1.0.7",
"axios-retry": "^4.1.0",
"cli-spinner": "^0.2.10",
"dotenv": "^16.4.5",
"hono": "^4.2.1",
"jest": "^29.7.0",
"regenerator-runtime": "^0.14.1",
"ts-jest": "^29.1.2"
"regenerator-runtime": "^0.14.1"
},
"devDependencies": {
"@types/cli-spinner": "^0.2.3",
"@types/jest": "^29.5.12",
"esbuild": "^0.20.2"
}
}
1 change: 0 additions & 1 deletion JS/edgechains/examples/chat-with-pdf/setupTests.js

This file was deleted.

4 changes: 2 additions & 2 deletions JS/edgechains/examples/chat-with-pdf/src/routes/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Supabase } from "arakoodev/vector-db";
import { PdfLoader } from "arakoodev/document-loader";
import { TextSplitter } from "arakoodev/splitter";
import { ArakooServer } from "arakoodev/arakooserver";
import { Spinner } from "cli-spinner";
import { Spinner } from "cli-spinner"
const server = new ArakooServer();

const __dirname = fileURLToPath(import.meta.url);
Expand Down Expand Up @@ -84,7 +84,7 @@ async function InsertToSupabase(content) {
}
}
// this should run only once for uploding pdf data to supabase then you can continue with the chatbot functionality
await InsertToSupabase(splitedDocs);
// await InsertToSupabase(splitedDocs);

ChatRouter.get("/", async (c) => {
const searchStr = c.req.query("question").toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,8 @@
import { ChatRouter } from "../routes/chat.js"; // Import the ChatRouter
import { Supabase } from "@arakoodev/vector-db"; // Import Supabase
import { test, expect } from 'vitest'

jest.mock("@arakoodev/vector-db", () => ({
Supabase: jest.fn().mockImplementation(() => ({
createClient: jest.fn().mockReturnValue({
getDataFromQuery: jest.fn().mockResolvedValue([]), // Mock the getDataFromQuery method
}),
})),
}));
test('should be return expected answer', async () => {
const res = await fetch("http://localhost:3000/chatWithpdf?question=who is Nirmala Sitharaman")

describe("ChatRouter", () => {
afterEach(() => {
jest.clearAllMocks(); // Clear mock function calls after each test
});

test("GET / should return response", async () => {
// Mock request context
const mockCtx = {
req: {
query: jest.fn().mockReturnValue({ question: "test question" }), // Mock query parameter
},
json: jest.fn(), // Mock json method
};

// Execute the route handler
await ChatRouter.get(mockCtx);

// Assertions
expect(mockCtx.req.query).toHaveBeenCalledWith("question"); // Check if query method was called with the correct parameter
expect(Supabase).toHaveBeenCalled(); // Check if Supabase constructor was called
expect(mockCtx.json).toHaveBeenCalledWith({ res: "mocked response" }); // Check if json method was called with the correct response
});

// Add more test cases as needed
});
const data = await res.json()
expect(data.res).toString()
}, 40000);
1 change: 0 additions & 1 deletion JS/edgechains/examples/chat-with-pdf/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"compilerOptions": {
"types": ["dotenv/config", "node"],
"target": "ES2022",
"module": "NodeNext",
"esModuleInterop": true,
Expand Down
7 changes: 7 additions & 0 deletions JS/edgechains/examples/chat-with-pdf/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { configDefaults, defineConfig } from 'vitest/config'

export default defineConfig({
test: {
exclude: [...configDefaults.exclude, 'packages/template/*'],
},
})
8 changes: 1 addition & 7 deletions JS/edgechains/examples/react-chain/.gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
# standard exclusions
node_modules

# build artifacts
dist

# environment files
.env
node_modules
Loading

0 comments on commit b0f3e34

Please sign in to comment.