Skip to content

Commit

Permalink
React-Chain-Code-Updated and Readme file Added
Browse files Browse the repository at this point in the history
  • Loading branch information
MSaifKhan01 committed Jan 23, 2024
1 parent 3945b81 commit 4127a06
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
9 changes: 6 additions & 3 deletions JS/edgechains/examples/react-chain/src/ReactChain.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { reactChainCall} from "./ReactChain";
// const { reactChainCall } = require('./ReactChain');
// Import the reactChainCall function from the ReactChain module
import { reactChainCall } from "./ReactChain";

// Describe block for the ReAct Chain test suite
describe("ReAct Chain", () => {
// Test case: It should return a response
it("should return a response", async () => {
// Call the reactChainCall function with a sample question
const response = await reactChainCall(
"Author David Chanoff has collaborated with a U.S. Navy admiral who served as the ambassador to the United Kingdom under which President?"
);
Expand All @@ -10,4 +14,3 @@ describe("ReAct Chain", () => {
expect(response).toContain("Bill Clinton");
}, 60000); // Increase the timeout to 60 seconds or as needed
});

18 changes: 8 additions & 10 deletions JS/edgechains/examples/react-chain/src/ReactChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dotenv.config();
// Create instances of Jsonnet, Hono, and OpenAiEndpoint
const jsonnet = new Jsonnet();
export const ReactChainRouter = new Hono();

const gpt3Endpoint = new OpenAiEndpoint(
"https://api.openai.com/v1/chat/completions",
process.env.OPENAI_API_KEY!,
Expand Down Expand Up @@ -41,11 +42,9 @@ export async function reactChainCall(query: string) {
// Make a GPT-3 call using the OpenAiEndpoint
const gptResponse = await gpt3Endpoint.gptFn(prompt);

// Replace escaped newline characters with actual newlines
const formattedResponse = gptResponse.replace(/\\n/g, '\n');

// Return the formatted response
return formattedResponse;

// Return the response
return gptResponse;

} catch (error) {
// Log and rethrow any errors that occur during the process
Expand All @@ -63,11 +62,10 @@ export function UserInput(query: string) {
// Call the reactChainCall function with the provided query
const ReactChainCall = await reactChainCall(query);

// Replace escaped newline characters with actual newlines in the response
const formattedResponse = ReactChainCall.replace(/\\n/g, '\n');


// Respond with the formatted answer
return res.json({ answer: formattedResponse });
// Respond with the ReactChainCall response
return res.json({ answer: ReactChainCall });

} catch (error) {
// If an error occurs, respond with an error status and message
Expand All @@ -77,7 +75,7 @@ export function UserInput(query: string) {
}

// Example usage: Make a UserInput call with a specific query
UserInput("what is AI");
UserInput("Author David Chanoff has collaborated with a U.S. Navy admiral who served as the ambassador to the United Kingdom under which President?");



Expand Down
2 changes: 1 addition & 1 deletion JS/edgechains/examples/react-chain/src/react-chain.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ local CUSTOM_TEMPLATE = |||
and Leonid Levin have the same type of work.
Action 3: Finish[yes]
Based upon the above prompt format, answer the user question in this format only. Doesn't stop in halfway.Give Full Response and give complete text
Based upon the above prompt format, answer the user question in this format only. Doesn't stop in halfway.each Thought , Action, Observation and Finish should be start from new line. Give Full Response and give complete text
Question: {}
|||;
Expand Down

0 comments on commit 4127a06

Please sign in to comment.