From 39173cca728af76a7abd0e37c95d79287a3d0fe2 Mon Sep 17 00:00:00 2001 From: Ferdous Bhai Date: Fri, 21 Apr 2023 16:24:17 +0800 Subject: [PATCH] Everything works --- .gitignore | 2 ++ README.md | 40 +++++++++++++++++++++++++++++++++++++++- config.ts | 11 +++++++++++ examples.ts | 35 +++++++++++++++++++++++++++++++++++ mod.ts | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 config.ts create mode 100644 examples.ts create mode 100644 mod.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a5c8936 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +.env \ No newline at end of file diff --git a/README.md b/README.md index 57d9b62..64a1bcb 100644 --- a/README.md +++ b/README.md @@ -1 +1,39 @@ -# conversation-summary \ No newline at end of file +# 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. +``` diff --git a/config.ts b/config.ts new file mode 100644 index 0000000..3e541c2 --- /dev/null +++ b/config.ts @@ -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); diff --git a/examples.ts b/examples.ts new file mode 100644 index 0000000..cab00a2 --- /dev/null +++ b/examples.ts @@ -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."), +); diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..3ded4d8 --- /dev/null +++ b/mod.ts @@ -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 { + // 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; + } +}