-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create some abstraction in llms
- Loading branch information
1 parent
4a976b0
commit 0a64bc4
Showing
11 changed files
with
162 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { OpenAIChat } from "langchain/llms/openai"; | ||
|
||
interface IAIModel { | ||
modelName: string; | ||
temperature: number; | ||
apiKey: string; | ||
} | ||
|
||
class AIModel { | ||
private model: OpenAIChat; | ||
|
||
constructor(options: IAIModel) { | ||
this.model = new OpenAIChat({ | ||
openAIApiKey: options.apiKey, | ||
modelName: options.modelName, | ||
temperature: options.temperature, | ||
}); | ||
} | ||
|
||
public async callModel(prompt: string): Promise<string> { | ||
return this.model.call(prompt); | ||
} | ||
} | ||
|
||
export default AIModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createSummary, processFeedbacks } from "./feedbackProcessor"; | ||
import AIModel from "./AIModel"; | ||
import { openAIApiKey } from "../../config"; | ||
|
||
export const askAi = async ( | ||
prompts: string[], | ||
modelName: string | ||
): Promise<string> => { | ||
console.info("Asking the experts..."); | ||
|
||
const model = new AIModel({ | ||
modelName: modelName, | ||
temperature: 0.0, | ||
apiKey: openAIApiKey(), | ||
}); | ||
|
||
const feedbacks = await processFeedbacks(model, prompts); | ||
|
||
const summary = await createSummary(model, feedbacks); | ||
|
||
return `${feedbacks.join("\n---\n")}\n\n---\n\n${summary}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import AIModel from "./AIModel"; | ||
import { completionPrompt } from "../constants"; | ||
|
||
interface IFeedback { | ||
feedback: string; | ||
} | ||
|
||
const collectAndLogFeedback = async ( | ||
feedbackPromise: Promise<string> | ||
): Promise<IFeedback> => { | ||
try { | ||
const feedback = await feedbackPromise; | ||
console.log(feedback); | ||
return { feedback }; | ||
} catch (error) { | ||
console.error(`Error in processing prompt`, error); | ||
throw error; | ||
} | ||
}; | ||
|
||
const createSummary = async ( | ||
model: AIModel, | ||
feedbacks: string[] | ||
): Promise<string> => { | ||
const finalPrompt = completionPrompt.replace( | ||
"{feedback}", | ||
feedbacks.join("\n---\n") | ||
); | ||
|
||
const summary = await model.callModel(finalPrompt); | ||
console.log(summary); | ||
return summary; | ||
}; | ||
|
||
const processFeedbacks = async ( | ||
model: AIModel, | ||
prompts: string[] | ||
): Promise<string[]> => { | ||
const feedbackPromises = prompts.map((prompt) => model.callModel(prompt)); | ||
|
||
const feedbackResults = await Promise.allSettled( | ||
feedbackPromises.map(collectAndLogFeedback) | ||
); | ||
|
||
const feedbacks = feedbackResults.reduce( | ||
(accumulatedFeedbacks, feedbackResult) => { | ||
if (feedbackResult.status === "fulfilled") { | ||
accumulatedFeedbacks.push(feedbackResult.value.feedback); | ||
} | ||
return accumulatedFeedbacks; | ||
}, | ||
[] as string[] | ||
); | ||
|
||
return feedbacks; | ||
}; | ||
|
||
export { createSummary, processFeedbacks }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
src/review/getFileNames.ts → src/review/prompt/getFileNames.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters