Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
Everything works
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdousbhai committed Apr 21, 2023
1 parent 9b2fb9e commit 39173cc
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
.env
40 changes: 39 additions & 1 deletion README.md
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.
```
11 changes: 11 additions & 0 deletions config.ts
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);
35 changes: 35 additions & 0 deletions examples.ts
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."),
);
34 changes: 34 additions & 0 deletions mod.ts
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;
}
}

0 comments on commit 39173cc

Please sign in to comment.