This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
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.
- Loading branch information
1 parent
9b2fb9e
commit 39173cc
Showing
5 changed files
with
121 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode | ||
.env |
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 |
---|---|---|
@@ -1 +1,39 @@ | ||
# conversation-summary | ||
# Summarize conversation with ChatGPT using ChatGPT! | ||
|
||
When interacting with large language models such as ChatGPT, a common task is to summarize a message, or a list of messages. This module provides a helper function `summarizeConversation` that does just that! | ||
|
||
### Usage | ||
``` | ||
import { summarizeConversation } from "./mod.ts"; | ||
await summarizeConversation([ | ||
{ | ||
role: "user", | ||
name: "John", | ||
content: "What is the meaning of life, the universe, and everything?", | ||
}, | ||
{ | ||
role: "assistant", | ||
name: "DAN", | ||
content: "42", | ||
}, | ||
]); // John asks the meaning of life, the universe, and everything, and Dan responds with "42." | ||
await summarizeConversation([ | ||
{ | ||
role: "user", | ||
content: "Doctor, I’m depressed. Life is harsh, unforgiving, cruel. ", | ||
}, | ||
{ | ||
role: "user", | ||
name: "Doctor", | ||
content: | ||
"The great clown Pagliacci is in town tonight. Go and see him! That should sort you out.", | ||
}, | ||
{ | ||
role: "user", | ||
content: "But doctor, I am Pagliacci.", | ||
}, | ||
], "A man is sad."); // The man expresses his sadness to a doctor, describing life as harsh, unforgiving, and cruel. The doctor suggests that he goes to see the great clown Pagliacci who is in town tonight, thinking it would help him feel better. The man responds with the revelation that he is Pagliacci, implying that he is the one who is supposed to bring joy to others, but he himself is unable to find happiness. | ||
``` |
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,11 @@ | ||
import { OpenAI } from "https://deno.land/x/openai@1.3.1/mod.ts"; | ||
import { load } from "https://deno.land/std@0.178.0/dotenv/mod.ts"; | ||
|
||
const env = await load(); | ||
const openAIKey = env["OPENAI_API_KEY"]; | ||
|
||
// Create OpenAI instance | ||
if (!openAIKey) { | ||
throw new Error("Please set OPENAI_API_KEY as an environment variable."); | ||
} | ||
export const openAI = new OpenAI(openAIKey); |
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,35 @@ | ||
import { summarizeConversation } from "./mod.ts"; | ||
|
||
console.log( | ||
await summarizeConversation([ | ||
{ | ||
role: "user", | ||
name: "John", | ||
content: "What is the meaning of life, the universe, and everything?", | ||
}, | ||
{ | ||
role: "assistant", | ||
name: "DAN", | ||
content: "42", | ||
}, | ||
]), | ||
); | ||
|
||
console.log( | ||
await summarizeConversation([ | ||
{ | ||
role: "user", | ||
content: "Doctor, I’m depressed. Life is harsh, unforgiving, cruel. ", | ||
}, | ||
{ | ||
role: "user", | ||
name: "Doctor", | ||
content: | ||
"The great clown Pagliacci is in town tonight. Go and see him! That should sort you out.", | ||
}, | ||
{ | ||
role: "user", | ||
content: "But doctor, I am Pagliacci.", | ||
}, | ||
], "A man is sad."), | ||
); |
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,34 @@ | ||
import { openAI } from "./config.ts"; | ||
|
||
export interface Message { | ||
role: "user" | "system" | "assistant"; | ||
name?: string; | ||
content: string; | ||
} | ||
|
||
export async function summarizeConversation( | ||
messages: Message[], | ||
previousSummary?: string, | ||
): Promise<string> { | ||
// Summarize conversation | ||
let systemPrompt = | ||
"You are capable of summarizing conversations minimizing any loss of information.\n\n"; | ||
systemPrompt += previousSummary | ||
? `Summarize the following conversation, providing lots of specific details from its context.\n\nContext:\n${previousSummary}\n\nConversation:\n` | ||
: "Summarize the following conversation."; | ||
|
||
try { | ||
const chatCompletion = await openAI.createChatCompletion({ | ||
model: "gpt-3.5-turbo", | ||
temperature: 0.6, | ||
messages: [ | ||
{ role: "system", content: systemPrompt }, | ||
...messages, | ||
], | ||
}); | ||
const assistantMessage = chatCompletion.choices[0].message; | ||
return assistantMessage.content; | ||
} catch (error) { | ||
return error.message; | ||
} | ||
} |