Skip to content

Commit

Permalink
adding langchain output parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhi1992002 committed Feb 17, 2024
1 parent b8235b3 commit e95a261
Show file tree
Hide file tree
Showing 23 changed files with 2,878 additions and 406 deletions.
51 changes: 51 additions & 0 deletions action/create-course.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use server";
import { OpenAI } from "@langchain/openai";
import { RunnableSequence } from "@langchain/core/runnables";
import { StructuredOutputParser } from "langchain/output_parsers";
import { PromptTemplate } from "@langchain/core/prompts";

export const createCourse = async ({
unit,
title,
}: {
unit: string;
title: string;
}) => {
try {
const parser = StructuredOutputParser.fromNamesAndDescriptions({
chapter_title: "one chapter in the unit",
youtube_search_query:
"best youtube query for the chapter_title so he can confirm his concepts",
});

const chain = RunnableSequence.from([
PromptTemplate.fromTemplate(
"Answer the users question as best as possible.\n{format_instructions}\n{question}"
),
new OpenAI({ temperature: 0 }),
parser,
]);

const request1 = chain.invoke({
question: `The title of course is ${title}, It has a unit named ${unit}. I want to create a chapter which tell about the basics of the unit , so name chapter accordingly`,
format_instructions: parser.getFormatInstructions(),
});

const request2 = chain.invoke({
question: `The title of course is ${title}, It has a unit named ${unit}. I want to create a chapter which tell about the intermediate stuff of the unit , so name chapter accordingly`,
format_instructions: parser.getFormatInstructions(),
});

const request3 = chain.invoke({
question: `The title of course is ${title}, It has a unit named ${unit}. I want to create a chapter which tell about the advance stuff of the unit , so name chapter accordingly`,
format_instructions: parser.getFormatInstructions(),
});

const courses = await Promise.all([request1, request2, request3]);

return { success: "success", courses: courses };
} catch (error) {
console.log(error);
return { error: error };
}
};
32 changes: 32 additions & 0 deletions action/create-image-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use server";
import { RunnableSequence } from "@langchain/core/runnables";
import { StructuredOutputParser } from "langchain/output_parsers";
import { PromptTemplate } from "@langchain/core/prompts";
import { OpenAI } from "@langchain/openai";

export const createImageQuery = async ({ title }: { title: string }) => {
try {
const parser = StructuredOutputParser.fromNamesAndDescriptions({
image_url:
"image url which suited my title and i can search it on unsplash",
});

const chain = RunnableSequence.from([
PromptTemplate.fromTemplate(
"Answer the users question as best as possible.\n{format_instructions}\n{question}"
),
new OpenAI({ temperature: 0 }),
parser,
]);

const request = await chain.invoke({
question: `create image for the titlr : ${title}`,
format_instructions: parser.getFormatInstructions(),
});

return { success: "success", image: request };
} catch (error) {
console.log(error);
return { error: error };
}
};
59 changes: 59 additions & 0 deletions action/create-questions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use server";
import { RunnableSequence } from "@langchain/core/runnables";
import { StructuredOutputParser } from "langchain/output_parsers";
import { PromptTemplate } from "@langchain/core/prompts";
import { OpenAI } from "@langchain/openai";

export const createQuestion = async ({
transcript,
segment,
}: {
transcript: string;
segment: "first" | "second" | "third" | "fourth";
}) => {
try {
const parser = StructuredOutputParser.fromNamesAndDescriptions({
question:
"relevant question for this chapter , create 4 option , out of which one is corrct and all other is wrong and put those into below keys ",
answer: "1st option , and this is right answer",
option1: "2nd option , and this option is wrong",
option2: "3rd option , and this option is wrong",
option3: "4th option , and this option is wrong",
});

const segmentLength = Math.floor(transcript.length / 4);
let segmentText = "";
switch (segment) {
case "first":
segmentText = transcript.slice(0, segmentLength);
break;
case "second":
segmentText = transcript.slice(segmentLength, 2 * segmentLength);
break;
case "third":
segmentText = transcript.slice(2 * segmentLength, 3 * segmentLength);
break;
case "fourth":
segmentText = transcript.slice(3 * segmentLength);
break;
}

const chain = RunnableSequence.from([
PromptTemplate.fromTemplate(
"Answer the users question as best as possible.\n{format_instructions}\n{question}"
),
new OpenAI({ temperature: 0, modelName: "gpt-3.5-turbo-1106" }),
parser,
]);

const request = await chain.invoke({
question: `Create a question and answer object with 4 options out of which one is correct. The topic is based on the following content segment: ${segmentText}`,
format_instructions: parser.getFormatInstructions(),
});

return { success: "success", questions: request };
} catch (error) {
console.log(error);
return { error: error };
}
};
33 changes: 33 additions & 0 deletions action/create-summary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use server";
import { RunnableSequence } from "@langchain/core/runnables";
import { StructuredOutputParser } from "langchain/output_parsers";
import { PromptTemplate } from "@langchain/core/prompts";
import { OpenAI } from "@langchain/openai";

export const createSummary = async ({ transcript }: { transcript: string }) => {
try {
const parser = StructuredOutputParser.fromNamesAndDescriptions({
summary: "a crisp summary of transcript that i will provide",
});

const chain = RunnableSequence.from([
PromptTemplate.fromTemplate(
"Answer the users question as best as possible.\n{format_instructions}\n{question}"
),
new OpenAI({ temperature: 0, modelName: "gpt-3.5-turbo-1106" }),
parser,
]);

const request = await chain.invoke({
question: `Create summary of youtube transcript in less than 300 words. Do not talk of sponsor or anything unrealted to main topic , also do not introduce what the summary is about.\n${transcript}`,
format_instructions: parser.getFormatInstructions(),
});

console.log(request);

return { success: "success", summary: request };
} catch (error) {
console.log(error);
return { error: error };
}
};
3 changes: 3 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const nextConfig = {
typescript: {
ignoreBuildErrors: true,
},
experimental: {
serverActions: true,
},

output: "standalone",
};
Expand Down
Loading

0 comments on commit e95a261

Please sign in to comment.