-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Ibtesam-Mahmood/chat
Added Chat Functionality
- Loading branch information
Showing
17 changed files
with
406 additions
and
61 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,68 @@ | ||
import * as readline from "readline"; | ||
|
||
interface ChatOptions { | ||
runner: (input: string, history: string[]) => Promise<string>; | ||
historyUpdate?: ( | ||
input: string, | ||
output: string, | ||
history: string[] | ||
) => string[]; | ||
inputTitle?: string; | ||
} | ||
|
||
function defaultHistoryUpdate( | ||
input: string, | ||
output: string, | ||
history: string[] | ||
): string[] { | ||
return [...history, `User: ${input}`, `Chat: ${output}`]; | ||
} | ||
|
||
async function cliChatHelper(options: ChatOptions): Promise<string[]> { | ||
const userInputString = `----------\n${ | ||
options.inputTitle ?? "Input" | ||
}:\n----------`; | ||
const chatInputString = `----------\nResponse:\n----------`; | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
// State | ||
let chatHistory: string[] = []; | ||
|
||
// start the chat | ||
console.log(); | ||
console.log('Type ".." to exit'); | ||
console.log(); | ||
console.log(userInputString); | ||
rl.on("line", async (input) => { | ||
// Exit the chat | ||
if (input === "..") { | ||
console.log(); | ||
console.log("Exiting chat"); | ||
rl.close(); | ||
return; | ||
} | ||
|
||
// Run the query | ||
console.log(); | ||
const result = await options.runner(input, chatHistory); | ||
|
||
// Print resopnse and next question prompt | ||
console.log(); | ||
console.log(chatInputString); | ||
console.log(result); | ||
console.log(); | ||
console.log(userInputString); | ||
|
||
// Update the chat history | ||
chatHistory = | ||
options.historyUpdate?.(input, result, chatHistory) ?? | ||
defaultHistoryUpdate(input, result, chatHistory); | ||
}); | ||
|
||
return chatHistory; | ||
} | ||
|
||
export { cliChatHelper }; |
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
File renamed without changes.
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,102 @@ | ||
import axios from "axios"; | ||
import { Tool } from "langchain/tools"; | ||
|
||
const value_serp_url = "https://api.valueserp.com/search"; | ||
|
||
class ValueSerpAPI extends Tool { | ||
name: string; | ||
description: string; | ||
apiKey: string; | ||
|
||
constructor(apiKey: string | undefined) { | ||
super(); | ||
|
||
if (!apiKey) { | ||
throw new Error("No apiKey provided"); | ||
} | ||
|
||
this.apiKey = apiKey as string; | ||
this.name = "search"; | ||
this.description = | ||
"a search engine. useful for when you need to answer questions about current events. input should be a search query."; | ||
} | ||
|
||
protected async _call(input: string): Promise<string> { | ||
const params = this.getParams(input); | ||
|
||
try { | ||
const response = await axios.get(value_serp_url, { params }); | ||
|
||
const data = response.data; | ||
return this.extractData(data); | ||
} catch (e) { | ||
throw new Error(`Got error from valueSerpAPI: ${e}`); | ||
} | ||
} | ||
|
||
private getParams(input: string): any { | ||
return { | ||
api_key: this.apiKey, | ||
q: input, | ||
hl: "en", | ||
google_domain: "google.com", | ||
gl: "us", | ||
}; | ||
} | ||
|
||
private extractData(data: any): string { | ||
let response: string = ""; | ||
|
||
// answer box | ||
if (data?.answer_box?.answers) { | ||
const answers = (data.answer_box.answers as any[]) | ||
.map((e) => `- ${e.answer}`) | ||
.join("\n"); | ||
|
||
response += "Possible Answers: \n" + answers + "\n\n"; | ||
} | ||
|
||
// knowledge graph | ||
if (data?.knowledge_graph?.description) { | ||
response += | ||
"Additional Information: \n- " + | ||
data.knowledge_graph.description + | ||
"\n\n"; | ||
} | ||
|
||
// QA results | ||
if (data?.related_questions) { | ||
const questions = data.related_questions.slice( | ||
0, | ||
Math.min(3, data.related_questions.length) | ||
); | ||
|
||
const results = questions | ||
.map((e: any) => `[Q]: ${e.question}\n[A]: ${e.answer}`) | ||
.join("\n---\n"); | ||
|
||
response += "Related Question Responses: \n" + results + "\n\n"; | ||
} | ||
|
||
// organic web results, only run if there is no response | ||
if (data?.organic_results) { | ||
const organgicResults = data.organic_results.slice( | ||
0, | ||
Math.min(3, data.organic_results.length) | ||
); | ||
const results = organgicResults | ||
.map((e: any) => `- [${e.title}]: ${e.snippet}`) | ||
.join("\n"); | ||
response += "Top Web Results: \n" + results + "\n\n"; | ||
} | ||
|
||
// defualt response | ||
if (response.length === 0) { | ||
return "No good search result found"; | ||
} | ||
|
||
return response; | ||
} | ||
} | ||
|
||
export default ValueSerpAPI; |
Oops, something went wrong.