From eec04383aa9e672481d97bc64a687244f0c08870 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Thu, 16 May 2024 16:32:44 -0400 Subject: [PATCH 01/13] replace 3.5 with 4o and use gemini flash 1.5 --- 04-call-summaries/llm.py | 10 ++- 04-call-summaries/run.py | 179 ++++++++++++++++++++++----------------- 2 files changed, 109 insertions(+), 80 deletions(-) diff --git a/04-call-summaries/llm.py b/04-call-summaries/llm.py index 1975b1c..44cc6f1 100644 --- a/04-call-summaries/llm.py +++ b/04-call-summaries/llm.py @@ -36,7 +36,7 @@ def ollama_client( def google_gemini_client( - model_name="gemini-pro", + model_name="gemini-1.5-flash-latest", prompt=None, settings=None, ): @@ -55,8 +55,8 @@ def google_gemini_client( def gpt3_5(prompt, model="gpt-3.5-turbo"): # Get API key from https://platform.openai.com/api-keys - OPEN_AI_API_KEY = os.environ.get("OPEN_AI_API_KEY") - openai_client = OpenAI(api_key=OPEN_AI_API_KEY) # Uses OPENAI_API_KEY + OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") + openai_client = OpenAI(api_key=OPENAI_API_KEY) # Uses OPENAI_API_KEY return ( openai_client.chat.completions.create( model=model, messages=[{"role": "user", "content": prompt}] @@ -70,6 +70,10 @@ def gpt_4_turbo(prompt): return gpt3_5(prompt, model="gpt-4-turbo") +def gpt_4o(prompt): + return gpt3_5(prompt, model="gpt-4o") + + def claude(prompt, model="claude-3-opus-20240229", max_tokens=1024): # Get API key from https://console.anthropic.com/settings/keys ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY") diff --git a/04-call-summaries/run.py b/04-call-summaries/run.py index 43c8eea..9fc07f8 100644 --- a/04-call-summaries/run.py +++ b/04-call-summaries/run.py @@ -1,10 +1,11 @@ from langchain_core.prompts import PromptTemplate -from llm import claude, google_gemini_client, gpt3_5, gpt_4_turbo, ollama_client +from llm import claude, google_gemini_client, gpt3_5, gpt_4_turbo, gpt_4o, ollama_client +from chunking import chunking_ingest # download transcripts from https://drive.google.com/drive/folders/19r6x3Zep4N9Rl_x4n4H6RpWkXviwbxyw?usp=sharing -def get_transcript(file_path="./transcript.txt"): +def get_transcript(file_path="./multi_benefit_transcript.txt"): file = open(file_path, encoding="utf-8") content = file.read() file.close() @@ -32,88 +33,112 @@ def get_transcript(file_path="./transcript.txt"): 8. Next Steps for Agent """ +CHUNKING_PROMPT = """ +You are a helpful AI assistant tasked with summarizing transcripts, however we can only process the transcripts in pieces. +Fill out the fields with the text given {text_given}. If the template already has the field filled out, do not overwrite this information. +If the template is blank, fill out the following template the best you can using the text: +1. Caller Information: +- Name +- Contact Information +- Availability +- Household Information +2. Reason/Type of Call: e.g., Applying for benefits, Follow-ups +3. Previous Benefits History: +- Applied for +- Receives +- Denied +4. Benefits Discussion: Prefix the discussed benefit with a hashtag (e.g., #SNAP, #LIHEAP) +5. Discussion Points: +- Key information points +6. Documents Needed: e.g., Income verification, Housing documentation +7. Next Steps for Client +8. Next Steps for Agent +""" + print(""" Select an llm 1. openhermes (default) 2. dolphin 3. gemini - 4. gpt 3.5 - 5. gpt 4 + 4. gpt 4 + 5. gpt 4o 6. claude 3 7. all """) - transcript = get_transcript() -llm = input() or "1" -prompt_template = PromptTemplate.from_template(PROMPT) -formatted_prompt = prompt_template.format(transcript=transcript) - -if llm == "2": - test = ollama_client(model_name="dolphin-mistral", prompt=formatted_prompt) - print("""---------- - Dolphin - """) -elif llm == "3": - test = google_gemini_client(prompt=formatted_prompt).text - print("""---------- - Gemini - """) -elif llm == "4": - print("""---------- - GPT 3.5 - """) - test = gpt3_5(prompt=formatted_prompt) -elif llm == "5": - print("""---------- - GPT 4 - """) - test = gpt_4_turbo(prompt=formatted_prompt) -elif llm == "6": - print("""---------- - Claude 3 - """) - test = claude(prompt=formatted_prompt) -elif llm == "7": - test_open_hermes = ollama_client(model_name="openhermes", prompt=formatted_prompt) - print(""" - Openhermes - """) - print(test_open_hermes) - - test_dolphin = ollama_client(model_name="dolphin-mistral", prompt=formatted_prompt) - print("""---------- - Dolphin - """) - print(test_dolphin) - - test_gemini = google_gemini_client(prompt=formatted_prompt).text - print("""---------- - Gemini - """) - print(test_gemini) - - print("""---------- - GPT 3.5 - """) - test_gpt3_5 = gpt3_5(prompt=formatted_prompt) - print(test_gpt3_5) - - print("""---------- - GPT 4 - """) - test_gpt4 = gpt_4_turbo(prompt=formatted_prompt) - print(test_gpt4) - - print("""---------- - Claude 3 - """) - test_claude = claude(prompt=formatted_prompt) - print(test_claude) -else: - test = ollama_client(model_name="openhermes", prompt=formatted_prompt) - print(""" - Openhermes - """) -if test: - print(test) +# llm = input() or "1" +# prompt_template = PromptTemplate.from_template(PROMPT) +# formatted_prompt = prompt_template.format(transcript=transcript) + +# if llm == "2": +# test = ollama_client(model_name="dolphin-mistral", prompt=formatted_prompt) +# print("""---------- +# Dolphin +# """) +# elif llm == "3": +# test = google_gemini_client(prompt=formatted_prompt).text +# print("""---------- +# Gemini Flash 1.5 +# """) +# elif llm == "4": +# print("""---------- +# GPT 4 +# """) +# test = gpt_4_turbo(prompt=formatted_prompt) +# elif llm == "5": +# print("""---------- +# GPT 4o +# """) +# test = gpt_4o(prompt=formatted_prompt) +# elif llm == "6": +# print("""---------- +# Claude 3 +# """) +# test = claude(prompt=formatted_prompt) +# elif llm == "7": +# test_open_hermes = ollama_client(model_name="openhermes", prompt=formatted_prompt) +# print(""" +# Openhermes +# """) +# print(test_open_hermes) + +# test_dolphin = ollama_client(model_name="dolphin-mistral", prompt=formatted_prompt) +# print("""---------- +# Dolphin +# """) +# print(test_dolphin) + +# test_gemini = google_gemini_client(prompt=formatted_prompt).text +# print("""---------- +# Gemini +# """) +# print(test_gemini) + +# print("""---------- +# GPT 4 +# """) +# test_gpt4 = gpt_4_turbo(prompt=formatted_prompt) +# print(test_gpt4) + +# print("""---------- +# GPT 4o +# """) +# test_gpt4o = gpt_4o(prompt=formatted_prompt) +# print(test_gpt4o) + +# print("""---------- +# Claude 3 +# """) +# test_claude = claude(prompt=formatted_prompt) +# print(test_claude) +# else: +# test = ollama_client(model_name="openhermes", prompt=formatted_prompt) +# print(""" +# Openhermes +# """) +# if test: +# print(test) + + +chunking_ingest(transcript=transcript, prompt=CHUNKING_PROMPT) From fa8961b7499fe7c821731b3bd33b42ff35e676e0 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Thu, 16 May 2024 16:33:53 -0400 Subject: [PATCH 02/13] format --- 04-call-summaries/run.py | 147 +++++++++++++++++++-------------------- 1 file changed, 72 insertions(+), 75 deletions(-) diff --git a/04-call-summaries/run.py b/04-call-summaries/run.py index 9fc07f8..f2a5e68 100644 --- a/04-call-summaries/run.py +++ b/04-call-summaries/run.py @@ -67,78 +67,75 @@ def get_transcript(file_path="./multi_benefit_transcript.txt"): """) transcript = get_transcript() -# llm = input() or "1" -# prompt_template = PromptTemplate.from_template(PROMPT) -# formatted_prompt = prompt_template.format(transcript=transcript) - -# if llm == "2": -# test = ollama_client(model_name="dolphin-mistral", prompt=formatted_prompt) -# print("""---------- -# Dolphin -# """) -# elif llm == "3": -# test = google_gemini_client(prompt=formatted_prompt).text -# print("""---------- -# Gemini Flash 1.5 -# """) -# elif llm == "4": -# print("""---------- -# GPT 4 -# """) -# test = gpt_4_turbo(prompt=formatted_prompt) -# elif llm == "5": -# print("""---------- -# GPT 4o -# """) -# test = gpt_4o(prompt=formatted_prompt) -# elif llm == "6": -# print("""---------- -# Claude 3 -# """) -# test = claude(prompt=formatted_prompt) -# elif llm == "7": -# test_open_hermes = ollama_client(model_name="openhermes", prompt=formatted_prompt) -# print(""" -# Openhermes -# """) -# print(test_open_hermes) - -# test_dolphin = ollama_client(model_name="dolphin-mistral", prompt=formatted_prompt) -# print("""---------- -# Dolphin -# """) -# print(test_dolphin) - -# test_gemini = google_gemini_client(prompt=formatted_prompt).text -# print("""---------- -# Gemini -# """) -# print(test_gemini) - -# print("""---------- -# GPT 4 -# """) -# test_gpt4 = gpt_4_turbo(prompt=formatted_prompt) -# print(test_gpt4) - -# print("""---------- -# GPT 4o -# """) -# test_gpt4o = gpt_4o(prompt=formatted_prompt) -# print(test_gpt4o) - -# print("""---------- -# Claude 3 -# """) -# test_claude = claude(prompt=formatted_prompt) -# print(test_claude) -# else: -# test = ollama_client(model_name="openhermes", prompt=formatted_prompt) -# print(""" -# Openhermes -# """) -# if test: -# print(test) - - -chunking_ingest(transcript=transcript, prompt=CHUNKING_PROMPT) +llm = input() or "1" +prompt_template = PromptTemplate.from_template(PROMPT) +formatted_prompt = prompt_template.format(transcript=transcript) + +if llm == "2": + test = ollama_client(model_name="dolphin-mistral", prompt=formatted_prompt) + print("""---------- + Dolphin + """) +elif llm == "3": + test = google_gemini_client(prompt=formatted_prompt).text + print("""---------- + Gemini Flash 1.5 + """) +elif llm == "4": + print("""---------- + GPT 4 + """) + test = gpt_4_turbo(prompt=formatted_prompt) +elif llm == "5": + print("""---------- + GPT 4o + """) + test = gpt_4o(prompt=formatted_prompt) +elif llm == "6": + print("""---------- + Claude 3 + """) + test = claude(prompt=formatted_prompt) +elif llm == "7": + test_open_hermes = ollama_client(model_name="openhermes", prompt=formatted_prompt) + print(""" + Openhermes + """) + print(test_open_hermes) + + test_dolphin = ollama_client(model_name="dolphin-mistral", prompt=formatted_prompt) + print("""---------- + Dolphin + """) + print(test_dolphin) + + test_gemini = google_gemini_client(prompt=formatted_prompt).text + print("""---------- + Gemini + """) + print(test_gemini) + + print("""---------- + GPT 4 + """) + test_gpt4 = gpt_4_turbo(prompt=formatted_prompt) + print(test_gpt4) + + print("""---------- + GPT 4o + """) + test_gpt4o = gpt_4o(prompt=formatted_prompt) + print(test_gpt4o) + + print("""---------- + Claude 3 + """) + test_claude = claude(prompt=formatted_prompt) + print(test_claude) +else: + test = ollama_client(model_name="openhermes", prompt=formatted_prompt) + print(""" + Openhermes + """) +if test: + print(test) From 61b4fd08da5278de244c301122a92e20dde19d71 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Thu, 16 May 2024 16:36:20 -0400 Subject: [PATCH 03/13] set up chunking function --- 04-call-summaries/chunking.py | 86 ++++++++++++++++++++++++++++++ 04-call-summaries/requirements.txt | 7 ++- 04-call-summaries/run.py | 2 + 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 04-call-summaries/chunking.py diff --git a/04-call-summaries/chunking.py b/04-call-summaries/chunking.py new file mode 100644 index 0000000..2c619ad --- /dev/null +++ b/04-call-summaries/chunking.py @@ -0,0 +1,86 @@ +from langchain_text_splitters import ( + RecursiveCharacterTextSplitter, + NLTKTextSplitter, + SpacyTextSplitter, +) +from llm import ollama_client + +# from langchain_openai import OpenAIEmbeddings +from langchain_community.embeddings.sentence_transformer import ( + SentenceTransformerEmbeddings, +) +from langchain.chains.conversational_retrieval.base import ConversationalRetrievalChain +from langchain.chains.llm import LLMChain +from langchain_core.prompts import PromptTemplate + +from langchain_community.vectorstores.faiss import FAISS +from langchain.docstore.document import Document + + +# split text into chunks +def get_text_chunks(text, chunk_size, chunk_overlap, text_splitter_choice): + if text_splitter_choice == "2": + text_splitter = NLTKTextSplitter() + elif text_splitter_choice == "3": + text_splitter = SpacyTextSplitter() + else: + text_splitter = RecursiveCharacterTextSplitter( + chunk_size=chunk_size, chunk_overlap=chunk_overlap + ) + + texts = text_splitter.split_text(text) + + docs = [ + Document( + page_content=t, + ) + for t in texts + ] + + return docs + + +CHUNKING_PROMPT = """ +You are a helpful AI assistant tasked with summarizing transcripts, however we can only process the transcripts in pieces. +Fill out the fields with the text given {text}. If the template {template} already has the field filled out, do not overwrite this information. +Please fill out the data with the following template: +1. Caller Information: +- Name +- Contact Information +- Availability +- Household Information +2. Reason/Type of Call: e.g., Applying for benefits, Follow-ups +3. Previous Benefits History: +- Applied for +- Receives +- Denied +4. Benefits Discussion: Prefix the discussed benefit with a hashtag (e.g., #SNAP, #LIHEAP) +5. Discussion Points: +- Key information points +6. Documents Needed: e.g., Income verification, Housing documentation +7. Next Steps for Client +8. Next Steps for Agent +""" + + +def chunking_ingest(transcript, prompt): + # text_splitter_choice= input() or "2" + text_chunks = get_text_chunks( + transcript, chunk_size=750, chunk_overlap=300, text_splitter_choice="2" + ) + + finalized_template = "" + prompt_template = PromptTemplate.from_template(prompt) + + print("TEXT CHUNK") + for text in text_chunks: + formatted_prompt = prompt_template.format( + text=text.page_content, template=finalized_template + ) + print("formatted_prompt", formatted_prompt) + answer = ollama_client(model_name="openhermes", prompt=formatted_prompt) + print("answer______________________") + print(answer) + finalized_template += "\n" + answer + print(finalized_template) + diff --git a/04-call-summaries/requirements.txt b/04-call-summaries/requirements.txt index 5b8da85..c986355 100644 --- a/04-call-summaries/requirements.txt +++ b/04-call-summaries/requirements.txt @@ -3,4 +3,9 @@ google-generativeai tokenizers langchain langchain_community -openai \ No newline at end of file +openai +sentence-transformers +nltk +spacy==3.7.4 +langchain-text-splitters +langchain_openai \ No newline at end of file diff --git a/04-call-summaries/run.py b/04-call-summaries/run.py index f2a5e68..684192d 100644 --- a/04-call-summaries/run.py +++ b/04-call-summaries/run.py @@ -139,3 +139,5 @@ def get_transcript(file_path="./multi_benefit_transcript.txt"): """) if test: print(test) + +chunking_ingest(transcript=transcript, prompt=CHUNKING_PROMPT) From 6b34a5beccfd14a84ee6822620b1db611a99b07c Mon Sep 17 00:00:00 2001 From: C Cheng Date: Fri, 17 May 2024 12:15:47 -0400 Subject: [PATCH 04/13] refactor to not instantiate with every message call --- 04-call-summaries/llm.py | 92 ++++++++++++++++--------------- 04-call-summaries/run.py | 113 +++++++++++++++++++-------------------- 2 files changed, 104 insertions(+), 101 deletions(-) diff --git a/04-call-summaries/llm.py b/04-call-summaries/llm.py index 44cc6f1..b186f54 100644 --- a/04-call-summaries/llm.py +++ b/04-call-summaries/llm.py @@ -16,10 +16,7 @@ def get_transcript(file_path="./transcript.txt"): def ollama_client( - model_name=None, - prompt=None, - callbacks=None, - settings=None, + model_name=None, prompt=None, callbacks=None, settings=None, client=None ): if not settings: settings = { @@ -32,13 +29,14 @@ def ollama_client( print("LLM settings:", model_name, settings) # To connect via another URL: Ollama(base_url='http://localhost:11434', ...) - return Ollama(model=model_name, callbacks=callbacks, **settings).invoke(prompt) + + if client: + return client.invoke(prompt) + return Ollama(model=model_name, callbacks=callbacks, **settings) def google_gemini_client( - model_name="gemini-1.5-flash-latest", - prompt=None, - settings=None, + model_name="gemini-1.5-flash-latest", prompt=None, settings=None, client=None ): # Get a Google API key by following the steps after clicking on Get an API key button # at https://ai.google.dev/tutorials/setup @@ -49,43 +47,49 @@ def google_gemini_client( genai.configure(api_key=GOOGLE_API_KEY) if settings: genai.GenerationConfig(**settings) - model = genai.GenerativeModel(model_name) - return model.generate_content(prompt) - - -def gpt3_5(prompt, model="gpt-3.5-turbo"): - # Get API key from https://platform.openai.com/api-keys - OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") - openai_client = OpenAI(api_key=OPENAI_API_KEY) # Uses OPENAI_API_KEY - return ( - openai_client.chat.completions.create( - model=model, messages=[{"role": "user", "content": prompt}] + if client: + return client.generate_content(prompt).text + return genai.GenerativeModel(model_name) + + +def gpt_client(prompt=None, model_choice="gpt-4o", client=None): + if client: + if model_choice == "gpt3": + model = "gpt-3.5-turbo" + elif model_choice == "gpt4": + model = "gpt-4-turbo" + else: + model = model_choice + return ( + client.chat.completions.create( + model=model, messages=[{"role": "user", "content": prompt}] + ) + .choices[0] + .message.content ) - .choices[0] - .message.content - ) - - -def gpt_4_turbo(prompt): - return gpt3_5(prompt, model="gpt-4-turbo") - + else: + # Get API key from https://platform.openai.com/api-keys + OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") + return OpenAI(api_key=OPENAI_API_KEY) # Uses OPENAI_API_KEY -def gpt_4o(prompt): - return gpt3_5(prompt, model="gpt-4o") +def claude_client( + prompt=None, model="claude-3-opus-20240229", max_tokens=1024, client=None +): + if client: + generated_response = client.messages.create( + model=model, + max_tokens=max_tokens, + messages=[{"role": "user", "content": prompt}], + ).content + text_response = "\n".join( + [text_block.text for text_block in generated_response] + ) -def claude(prompt, model="claude-3-opus-20240229", max_tokens=1024): - # Get API key from https://console.anthropic.com/settings/keys - ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY") - - client = anthropic.Anthropic( - api_key=ANTHROPIC_API_KEY, - ) - generated_response = client.messages.create( - model=model, - max_tokens=max_tokens, - messages=[{"role": "user", "content": prompt}], - ).content - text_response = "\n".join([text_block.text for text_block in generated_response]) - - return text_response + return text_response + else: + # Get API key from https://console.anthropic.com/settings/keys + ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY") + return anthropic.Anthropic( + api_key=ANTHROPIC_API_KEY, + ) diff --git a/04-call-summaries/run.py b/04-call-summaries/run.py index 684192d..525cd24 100644 --- a/04-call-summaries/run.py +++ b/04-call-summaries/run.py @@ -1,11 +1,11 @@ from langchain_core.prompts import PromptTemplate -from llm import claude, google_gemini_client, gpt3_5, gpt_4_turbo, gpt_4o, ollama_client +from llm import claude_client, google_gemini_client, gpt_client, ollama_client from chunking import chunking_ingest # download transcripts from https://drive.google.com/drive/folders/19r6x3Zep4N9Rl_x4n4H6RpWkXviwbxyw?usp=sharing -def get_transcript(file_path="./multi_benefit_transcript.txt"): +def get_transcript(file_path="./transcript.txt"): file = open(file_path, encoding="utf-8") content = file.read() file.close() @@ -35,24 +35,8 @@ def get_transcript(file_path="./multi_benefit_transcript.txt"): CHUNKING_PROMPT = """ You are a helpful AI assistant tasked with summarizing transcripts, however we can only process the transcripts in pieces. -Fill out the fields with the text given {text_given}. If the template already has the field filled out, do not overwrite this information. -If the template is blank, fill out the following template the best you can using the text: -1. Caller Information: -- Name -- Contact Information -- Availability -- Household Information -2. Reason/Type of Call: e.g., Applying for benefits, Follow-ups -3. Previous Benefits History: -- Applied for -- Receives -- Denied -4. Benefits Discussion: Prefix the discussed benefit with a hashtag (e.g., #SNAP, #LIHEAP) -5. Discussion Points: -- Key information points -6. Documents Needed: e.g., Income verification, Housing documentation -7. Next Steps for Client -8. Next Steps for Agent +Fill out the fields with the text given {text}. If the following template already has the field filled out, do not overwrite this information. +Please fill out the data with the following template: {template} """ print(""" @@ -72,12 +56,14 @@ def get_transcript(file_path="./multi_benefit_transcript.txt"): formatted_prompt = prompt_template.format(transcript=transcript) if llm == "2": - test = ollama_client(model_name="dolphin-mistral", prompt=formatted_prompt) + ollama = ollama_client(model_name="dolphin-mistral") + response = ollama_client(client=ollama, prompt=formatted_prompt) print("""---------- Dolphin """) elif llm == "3": - test = google_gemini_client(prompt=formatted_prompt).text + gemini = google_gemini_client() + response = google_gemini_client(client=gemini, prompt=formatted_prompt) print("""---------- Gemini Flash 1.5 """) @@ -85,59 +71,72 @@ def get_transcript(file_path="./multi_benefit_transcript.txt"): print("""---------- GPT 4 """) - test = gpt_4_turbo(prompt=formatted_prompt) + gpt = gpt_client() + response = gpt_client(client=gpt, model_choice="gpt4", prompt=formatted_prompt) elif llm == "5": print("""---------- GPT 4o """) - test = gpt_4o(prompt=formatted_prompt) + gpt = gpt_client() + response = gpt_client(client=gpt, model_choice="gpt4o", prompt=formatted_prompt) elif llm == "6": print("""---------- Claude 3 """) - test = claude(prompt=formatted_prompt) + claude = claude_client() + response = claude_client(client=claude, prompt=formatted_prompt) elif llm == "7": - test_open_hermes = ollama_client(model_name="openhermes", prompt=formatted_prompt) - print(""" - Openhermes - """) - print(test_open_hermes) + # print(""" + # Openhermes + # """) + # ollama_openhermes = ollama_client(model_name="openhermes") + # ollama_openhermes_response = ollama_client( + # client=ollama_openhermes, prompt=formatted_prompt + # ) + # print(ollama_openhermes_response) - test_dolphin = ollama_client(model_name="dolphin-mistral", prompt=formatted_prompt) - print("""---------- - Dolphin - """) - print(test_dolphin) + # print("""---------- + # Dolphin + # """) + # ollama_dolphin = ollama_client(model_name="dolphin-mistral") + # dolphin_response = ollama_client(client=ollama_dolphin, prompt=formatted_prompt) + # print(dolphin_response) - test_gemini = google_gemini_client(prompt=formatted_prompt).text - print("""---------- - Gemini - """) - print(test_gemini) + # print("""---------- + # Gemini Flash 1.5 + # """) + # gemini = google_gemini_client() + # gemini_response = google_gemini_client(client=gemini, prompt=formatted_prompt) + # print(gemini_response) - print("""---------- - GPT 4 - """) - test_gpt4 = gpt_4_turbo(prompt=formatted_prompt) - print(test_gpt4) + # gpt = gpt_client() + # print("""---------- + # GPT 4 + # """) + # gpt_4_response = gpt_client( + # client=gpt, model_choice="gpt4", prompt=formatted_prompt + # ) + # print(gpt_4_response) - print("""---------- - GPT 4o - """) - test_gpt4o = gpt_4o(prompt=formatted_prompt) - print(test_gpt4o) + # print("""---------- + # GPT 4o + # """) + # gpt_4o_response = gpt_client( + # client=gpt, model_choice="gpt-4o", prompt=formatted_prompt + # ) + # print(gpt_4o_response) print("""---------- Claude 3 """) - test_claude = claude(prompt=formatted_prompt) - print(test_claude) + claude = claude_client() + claude_response = claude_client(client=claude, prompt=formatted_prompt) + print(claude_response) else: - test = ollama_client(model_name="openhermes", prompt=formatted_prompt) + ollama = ollama_client(model_name="openhermes") + response = ollama_client(client=ollama, prompt=formatted_prompt) print(""" Openhermes """) -if test: - print(test) - -chunking_ingest(transcript=transcript, prompt=CHUNKING_PROMPT) +if response: + print(response) From 693052020649cb5fdec2b8dfd5ad0237215d1acd Mon Sep 17 00:00:00 2001 From: C Cheng Date: Fri, 17 May 2024 14:08:46 -0400 Subject: [PATCH 05/13] create function for stuffing code --- 04-call-summaries/run.py | 182 +++++++++++++++++++-------------------- 1 file changed, 91 insertions(+), 91 deletions(-) diff --git a/04-call-summaries/run.py b/04-call-summaries/run.py index 525cd24..bbd04c2 100644 --- a/04-call-summaries/run.py +++ b/04-call-summaries/run.py @@ -39,104 +39,104 @@ def get_transcript(file_path="./transcript.txt"): Please fill out the data with the following template: {template} """ -print(""" - Select an llm - 1. openhermes (default) - 2. dolphin - 3. gemini - 4. gpt 4 - 5. gpt 4o - 6. claude 3 - 7. all - """) transcript = get_transcript() -llm = input() or "1" prompt_template = PromptTemplate.from_template(PROMPT) formatted_prompt = prompt_template.format(transcript=transcript) -if llm == "2": - ollama = ollama_client(model_name="dolphin-mistral") - response = ollama_client(client=ollama, prompt=formatted_prompt) - print("""---------- - Dolphin + +def stuffing_summary(prompt=None): + print(""" + Select an llm + 1. openhermes (default) + 2. dolphin + 3. gemini + 4. gpt 4 + 5. gpt 4o + 6. claude 3 + 7. all """) -elif llm == "3": - gemini = google_gemini_client() - response = google_gemini_client(client=gemini, prompt=formatted_prompt) - print("""---------- - Gemini Flash 1.5 + + llm = input() or "1" + + if llm == "2": + ollama = ollama_client(model_name="dolphin-mistral") + response = ollama_client(client=ollama, prompt=prompt) + print("""---------- + Dolphin + """) + elif llm == "3": + gemini = google_gemini_client() + response = google_gemini_client(client=gemini, prompt=prompt) + print("""---------- + Gemini Flash 1.5 + """) + elif llm == "4": + print("""---------- + GPT 4 """) -elif llm == "4": - print("""---------- - GPT 4 - """) - gpt = gpt_client() - response = gpt_client(client=gpt, model_choice="gpt4", prompt=formatted_prompt) -elif llm == "5": - print("""---------- - GPT 4o - """) - gpt = gpt_client() - response = gpt_client(client=gpt, model_choice="gpt4o", prompt=formatted_prompt) -elif llm == "6": - print("""---------- - Claude 3 + gpt = gpt_client() + response = gpt_client(client=gpt, model_choice="gpt4", prompt=prompt) + elif llm == "5": + print("""---------- + GPT 4o """) - claude = claude_client() - response = claude_client(client=claude, prompt=formatted_prompt) -elif llm == "7": - # print(""" - # Openhermes - # """) - # ollama_openhermes = ollama_client(model_name="openhermes") - # ollama_openhermes_response = ollama_client( - # client=ollama_openhermes, prompt=formatted_prompt - # ) - # print(ollama_openhermes_response) - - # print("""---------- - # Dolphin - # """) - # ollama_dolphin = ollama_client(model_name="dolphin-mistral") - # dolphin_response = ollama_client(client=ollama_dolphin, prompt=formatted_prompt) - # print(dolphin_response) - - # print("""---------- - # Gemini Flash 1.5 - # """) - # gemini = google_gemini_client() - # gemini_response = google_gemini_client(client=gemini, prompt=formatted_prompt) - # print(gemini_response) - - # gpt = gpt_client() - # print("""---------- - # GPT 4 - # """) - # gpt_4_response = gpt_client( - # client=gpt, model_choice="gpt4", prompt=formatted_prompt - # ) - # print(gpt_4_response) - - # print("""---------- - # GPT 4o - # """) - # gpt_4o_response = gpt_client( - # client=gpt, model_choice="gpt-4o", prompt=formatted_prompt - # ) - # print(gpt_4o_response) - - print("""---------- - Claude 3 + gpt = gpt_client() + response = gpt_client(client=gpt, model_choice="gpt4o", prompt=prompt) + elif llm == "6": + print("""---------- + Claude 3 + """) + claude = claude_client() + response = claude_client(client=claude, prompt=prompt) + elif llm == "7": + print(""" + Openhermes + """) + ollama_openhermes = ollama_client(model_name="openhermes") + ollama_openhermes_response = ollama_client( + client=ollama_openhermes, prompt=prompt + ) + print(ollama_openhermes_response) + + print("""---------- + Dolphin + """) + ollama_dolphin = ollama_client(model_name="dolphin-mistral") + dolphin_response = ollama_client(client=ollama_dolphin, prompt=prompt) + print(dolphin_response) + + print("""---------- + Gemini Flash 1.5 + """) + gemini = google_gemini_client() + gemini_response = google_gemini_client(client=gemini, prompt=prompt) + print(gemini_response) + + gpt = gpt_client() + print("""---------- + GPT 4 """) - claude = claude_client() - claude_response = claude_client(client=claude, prompt=formatted_prompt) - print(claude_response) -else: - ollama = ollama_client(model_name="openhermes") - response = ollama_client(client=ollama, prompt=formatted_prompt) - print(""" - Openhermes + gpt_4_response = gpt_client(client=gpt, model_choice="gpt4", prompt=prompt) + print(gpt_4_response) + + print("""---------- + GPT 4o """) -if response: - print(response) + gpt_4o_response = gpt_client(client=gpt, model_choice="gpt-4o", prompt=prompt) + print(gpt_4o_response) + + print("""---------- + Claude 3 + """) + claude = claude_client() + claude_response = claude_client(client=claude, prompt=prompt) + print(claude_response) + else: + ollama = ollama_client(model_name="openhermes") + response = ollama_client(client=ollama, prompt=prompt) + print(""" + Openhermes + """) + if response: + print(response) From 93af4ab711688b758a1151480c2cc9638bc3e457 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Fri, 17 May 2024 16:25:23 -0400 Subject: [PATCH 06/13] implement chunking code --- 04-call-summaries/chunking.py | 123 +++++++++++++++++++++++++++------- 04-call-summaries/run.py | 7 -- 2 files changed, 97 insertions(+), 33 deletions(-) diff --git a/04-call-summaries/chunking.py b/04-call-summaries/chunking.py index 2c619ad..8076e1a 100644 --- a/04-call-summaries/chunking.py +++ b/04-call-summaries/chunking.py @@ -3,18 +3,10 @@ NLTKTextSplitter, SpacyTextSplitter, ) -from llm import ollama_client - -# from langchain_openai import OpenAIEmbeddings -from langchain_community.embeddings.sentence_transformer import ( - SentenceTransformerEmbeddings, -) -from langchain.chains.conversational_retrieval.base import ConversationalRetrievalChain -from langchain.chains.llm import LLMChain from langchain_core.prompts import PromptTemplate - -from langchain_community.vectorstores.faiss import FAISS from langchain.docstore.document import Document +from llm import google_gemini_client, claude_client, gpt_client, ollama_client +from run import get_transcript # split text into chunks @@ -42,8 +34,11 @@ def get_text_chunks(text, chunk_size, chunk_overlap, text_splitter_choice): CHUNKING_PROMPT = """ You are a helpful AI assistant tasked with summarizing transcripts, however we can only process the transcripts in pieces. -Fill out the fields with the text given {text}. If the template {template} already has the field filled out, do not overwrite this information. -Please fill out the data with the following template: +Fill out the fields with the text given {text}. If the following template already has the field filled out, do not overwrite this information. +Please fill out the data with the following template: {template} +""" + +initial_temp = """ 1. Caller Information: - Name - Contact Information @@ -64,23 +59,99 @@ def get_text_chunks(text, chunk_size, chunk_overlap, text_splitter_choice): def chunking_ingest(transcript, prompt): - # text_splitter_choice= input() or "2" text_chunks = get_text_chunks( transcript, chunk_size=750, chunk_overlap=300, text_splitter_choice="2" ) - - finalized_template = "" prompt_template = PromptTemplate.from_template(prompt) + template = initial_temp + + print(""" + Select an llm + 1. openhermes (default) + 2. dolphin + 3. gemini + 4. gpt 4 + 5. gpt 4o + 6. claude 3 + """) + + llm = input() or "1" + + if llm == "2": + client = ollama_client(model_name="dolphin-mistral") + print("""---------- + Dolphin + """) + for text in text_chunks: + formatted_prompt = prompt_template.format( + text=text.page_content, template=template + ) + print("----------------------") + template = ollama_client(client=client, prompt=formatted_prompt) + return template + elif llm == "3": + gemini = google_gemini_client() + print("""---------- + Gemini Flash 1.5 + """) + for text in text_chunks: + formatted_prompt = prompt_template.format( + text=text.page_content, template=template + ) + print("----------------------") + template = google_gemini_client(client=gemini, prompt=formatted_prompt) + return template + elif llm == "4": + print("""---------- + GPT 4 + """) + gpt = gpt_client() + for text in text_chunks: + formatted_prompt = prompt_template.format( + text=text.page_content, template=template + ) + print("----------------------") + template = gpt_client( + client=gpt, model_choice="gpt4", prompt=formatted_prompt + ) + return template + elif llm == "5": + print("""---------- + GPT 4o + """) + for text in text_chunks: + formatted_prompt = prompt_template.format( + text=text.page_content, template=template + ) + print("----------------------") + template = gpt_client( + client=gpt, model_choice="gpt-4o", prompt=formatted_prompt + ) + return template + elif llm == "6": + print("""---------- + Claude 3 + """) + claude = claude_client() + for text in text_chunks: + formatted_prompt = prompt_template.format( + text=text.page_content, template=template + ) + print("----------------------") + template = claude_client(client=claude, prompt=formatted_prompt) + return template + else: + print(""" + Openhermes + """) + ollama = ollama_client(model_name="openhermes") + for text in text_chunks: + formatted_prompt = prompt_template.format( + text=text.page_content, template=template + ) + print("----------------------") + template = ollama_client(client=ollama, prompt=formatted_prompt) + return template - print("TEXT CHUNK") - for text in text_chunks: - formatted_prompt = prompt_template.format( - text=text.page_content, template=finalized_template - ) - print("formatted_prompt", formatted_prompt) - answer = ollama_client(model_name="openhermes", prompt=formatted_prompt) - print("answer______________________") - print(answer) - finalized_template += "\n" + answer - print(finalized_template) +print(chunking_ingest(transcript=get_transcript(), prompt=CHUNKING_PROMPT)) diff --git a/04-call-summaries/run.py b/04-call-summaries/run.py index bbd04c2..0d23d4d 100644 --- a/04-call-summaries/run.py +++ b/04-call-summaries/run.py @@ -1,7 +1,6 @@ from langchain_core.prompts import PromptTemplate from llm import claude_client, google_gemini_client, gpt_client, ollama_client -from chunking import chunking_ingest # download transcripts from https://drive.google.com/drive/folders/19r6x3Zep4N9Rl_x4n4H6RpWkXviwbxyw?usp=sharing @@ -33,12 +32,6 @@ def get_transcript(file_path="./transcript.txt"): 8. Next Steps for Agent """ -CHUNKING_PROMPT = """ -You are a helpful AI assistant tasked with summarizing transcripts, however we can only process the transcripts in pieces. -Fill out the fields with the text given {text}. If the following template already has the field filled out, do not overwrite this information. -Please fill out the data with the following template: {template} -""" - transcript = get_transcript() prompt_template = PromptTemplate.from_template(PROMPT) From 11e9db10a1bb0f9ad4a8ca298bf7ec4be6e13768 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Fri, 17 May 2024 16:33:37 -0400 Subject: [PATCH 07/13] fix instantiation err --- 04-call-summaries/chunking.py | 1 + 1 file changed, 1 insertion(+) diff --git a/04-call-summaries/chunking.py b/04-call-summaries/chunking.py index 8076e1a..03e0812 100644 --- a/04-call-summaries/chunking.py +++ b/04-call-summaries/chunking.py @@ -119,6 +119,7 @@ def chunking_ingest(transcript, prompt): print("""---------- GPT 4o """) + gpt = gpt_client() for text in text_chunks: formatted_prompt = prompt_template.format( text=text.page_content, template=template From 26a079a39ce4cbede253c2b3cba5a0df8afca039 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Tue, 21 May 2024 10:52:48 -0400 Subject: [PATCH 08/13] refactor llm choices into class --- 04-call-summaries/llm.py | 173 +++++++++++++++++++++------------------ 1 file changed, 92 insertions(+), 81 deletions(-) diff --git a/04-call-summaries/llm.py b/04-call-summaries/llm.py index b186f54..31990f5 100644 --- a/04-call-summaries/llm.py +++ b/04-call-summaries/llm.py @@ -9,87 +9,98 @@ dotenv.load_dotenv() -def get_transcript(file_path="./transcript.txt"): - file = open(file_path, encoding="utf-8") - content = file.read() - return content - - -def ollama_client( - model_name=None, prompt=None, callbacks=None, settings=None, client=None -): - if not settings: - settings = { - # "temperature": 0.1, - # "system": "", - # "template": "", - # See https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/llms/ollama.py - "stop": None - } - - print("LLM settings:", model_name, settings) - # To connect via another URL: Ollama(base_url='http://localhost:11434', ...) - - if client: - return client.invoke(prompt) - return Ollama(model=model_name, callbacks=callbacks, **settings) - - -def google_gemini_client( - model_name="gemini-1.5-flash-latest", prompt=None, settings=None, client=None -): - # Get a Google API key by following the steps after clicking on Get an API key button - # at https://ai.google.dev/tutorials/setup - GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") - - print("LLM settings:", model_name, settings) - - genai.configure(api_key=GOOGLE_API_KEY) - if settings: - genai.GenerationConfig(**settings) - if client: - return client.generate_content(prompt).text - return genai.GenerativeModel(model_name) - - -def gpt_client(prompt=None, model_choice="gpt-4o", client=None): - if client: - if model_choice == "gpt3": - model = "gpt-3.5-turbo" - elif model_choice == "gpt4": - model = "gpt-4-turbo" - else: - model = model_choice - return ( - client.chat.completions.create( - model=model, messages=[{"role": "user", "content": prompt}] +class LLM: + def __init__( + self, + client_name=None, + model_name=None, + max_tokens=1024, + settings=None, + ): + self.client_name = client_name + """Name of llm selection""" + self.model_name = model_name + """User friendly model name""" + self.model_version = model_name + """Exact model name being passed into the initializer""" + self.max_tokens = max_tokens + self.client = None + self.settings = settings + + def get_client(self): + """Retrieves the llm client""" + if self.client_name == "ollama": + if not settings: + settings = { + # "temperature": 0.1, + # "system": "", + # "template": "", + # See https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/llms/ollama.py + "stop": None + } + if self.model_name is None: + self.model_name = "openhermes" + # To connect via another URL: Ollama(base_url='http://localhost:11434', ...) + self.client = Ollama(model=self.model_version, **self.settings) + + elif self.client_name == "gemini": + # Get a Google API key by following the steps after clicking on Get an API key button + # at https://ai.google.dev/tutorials/setup + GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") + print("LLM settings:", self.model_name, settings) + + if self.model_version is None: + self.model_version = "gemini-1.5-flash-latest" + elif self.model_name == "gemini-pro": + self.model_version = "gemini-1.5-pro-latest" + + genai.configure(api_key=GOOGLE_API_KEY) + if settings: + genai.GenerationConfig(**self.settings) + self.client = genai.GenerativeModel(self.model_name) + + elif self.client_name == "gpt": + if self.model_name is None: + self.model_version = "gpt-4o" + elif self.model_name == "gpt3": + self.model_version = "gpt-3.5-turbo" + elif self.model_name == "gpt4": + self.model_version = "gpt-4-turbo" + + # Get API key from https://platform.openai.com/api-keys + OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") + self.client = OpenAI(api_key=OPENAI_API_KEY) # Uses OPENAI_API_KEY + + elif self.client_name == "claude": + self.model_version = "claude-3-opus-20240229" + ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY") + self.client = anthropic.Anthropic( + api_key=ANTHROPIC_API_KEY, ) - .choices[0] - .message.content - ) - else: - # Get API key from https://platform.openai.com/api-keys - OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") - return OpenAI(api_key=OPENAI_API_KEY) # Uses OPENAI_API_KEY + def generate_text(self, prompt=None): + """Generates response given prompt""" + if self.client_name == "ollama": + return self.client.invoke(prompt) + elif self.client_name == "gemini": + return self.client.generate(prompt).text + elif self.client_name == "gpt": + return ( + self.client.chat.completions.create( + model=self.model_version, + messages=[{"role": "user", "content": prompt}], + ) + .choices[0] + .message.content + ) + elif self.client_name == "claude": + generated_response = self.client.messages.create( + model=self.model_version, + max_tokens=self.max_tokens, + messages=[{"role": "user", "content": prompt}], + ).content + text_response = "\n".join( + [text_block.text for text_block in generated_response] + ) -def claude_client( - prompt=None, model="claude-3-opus-20240229", max_tokens=1024, client=None -): - if client: - generated_response = client.messages.create( - model=model, - max_tokens=max_tokens, - messages=[{"role": "user", "content": prompt}], - ).content - text_response = "\n".join( - [text_block.text for text_block in generated_response] - ) - - return text_response - else: - # Get API key from https://console.anthropic.com/settings/keys - ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY") - return anthropic.Anthropic( - api_key=ANTHROPIC_API_KEY, - ) + return text_response From 94fbc1b848d8332780f3908eba329a5a09c92a62 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Tue, 21 May 2024 11:03:03 -0400 Subject: [PATCH 09/13] update stuffing code --- 04-call-summaries/run.py | 51 ++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/04-call-summaries/run.py b/04-call-summaries/run.py index 0d23d4d..ef790ec 100644 --- a/04-call-summaries/run.py +++ b/04-call-summaries/run.py @@ -1,6 +1,6 @@ from langchain_core.prompts import PromptTemplate -from llm import claude_client, google_gemini_client, gpt_client, ollama_client +from llm import LLM # download transcripts from https://drive.google.com/drive/folders/19r6x3Zep4N9Rl_x4n4H6RpWkXviwbxyw?usp=sharing @@ -53,14 +53,14 @@ def stuffing_summary(prompt=None): llm = input() or "1" if llm == "2": - ollama = ollama_client(model_name="dolphin-mistral") - response = ollama_client(client=ollama, prompt=prompt) + ollama = LLM(client_name="ollama", model_name="dolphin-mistral") + response = ollama.generate_text(prompt=prompt) print("""---------- Dolphin """) elif llm == "3": - gemini = google_gemini_client() - response = google_gemini_client(client=gemini, prompt=prompt) + gemini = LLM(client_name="gemini") + response = gemini.generate_text(prompt=prompt) print("""---------- Gemini Flash 1.5 """) @@ -68,66 +68,65 @@ def stuffing_summary(prompt=None): print("""---------- GPT 4 """) - gpt = gpt_client() - response = gpt_client(client=gpt, model_choice="gpt4", prompt=prompt) + gpt = LLM(client_name="gpt", model_name="gpt4") + response = gpt.generate_text(prompt=prompt) elif llm == "5": print("""---------- GPT 4o """) - gpt = gpt_client() - response = gpt_client(client=gpt, model_choice="gpt4o", prompt=prompt) + gpt = LLM(client_name="gpt", model_name="gpt4o") + response = gpt.generate_text(prompt=prompt) elif llm == "6": print("""---------- Claude 3 """) - claude = claude_client() - response = claude_client(client=claude, prompt=prompt) + claude = LLM(client_name="claude") + response = claude.generate_text(prompt=prompt) elif llm == "7": print(""" Openhermes """) - ollama_openhermes = ollama_client(model_name="openhermes") - ollama_openhermes_response = ollama_client( - client=ollama_openhermes, prompt=prompt - ) + ollama_openhermes = LLM(client_name="ollama", model_name="openhermes") + ollama_openhermes_response = ollama_openhermes.generate_text(prompt=prompt) print(ollama_openhermes_response) print("""---------- Dolphin """) - ollama_dolphin = ollama_client(model_name="dolphin-mistral") - dolphin_response = ollama_client(client=ollama_dolphin, prompt=prompt) + ollama_dolphin = LLM(client_name="ollama", model_name="dolphin-mistral") + dolphin_response = ollama_dolphin.generate_text(prompt=prompt) print(dolphin_response) print("""---------- Gemini Flash 1.5 """) - gemini = google_gemini_client() - gemini_response = google_gemini_client(client=gemini, prompt=prompt) + gemini = LLM(client_name="gemini") + gemini_response = gemini.generate_text(prompt=prompt) print(gemini_response) - gpt = gpt_client() print("""---------- GPT 4 """) - gpt_4_response = gpt_client(client=gpt, model_choice="gpt4", prompt=prompt) + gpt_4 = LLM(client_name="gpt", model_name="gpt4") + gpt_4_response = gpt_4.generate_text(prompt=prompt) print(gpt_4_response) print("""---------- GPT 4o """) - gpt_4o_response = gpt_client(client=gpt, model_choice="gpt-4o", prompt=prompt) + gpt_4o = LLM(client_name="_4o", model_name="gpt4o") + gpt_4o_response = gpt_4o.generate_text(prompt=prompt) print(gpt_4o_response) print("""---------- Claude 3 """) - claude = claude_client() - claude_response = claude_client(client=claude, prompt=prompt) + claude = LLM(client_name="claude") + claude_response = claude.generate_text(prompt=prompt) print(claude_response) else: - ollama = ollama_client(model_name="openhermes") - response = ollama_client(client=ollama, prompt=prompt) + ollama = LLM(client_name="ollama", model_name="openhermes") + response = ollama.generate_text(prompt=prompt) print(""" Openhermes """) From 460fc0933e6c644e7fb931d30f7162233eb67a86 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Tue, 21 May 2024 12:30:12 -0400 Subject: [PATCH 10/13] get client call for responses --- 04-call-summaries/chunking.py | 73 +++++++++-------------------------- 04-call-summaries/llm.py | 14 +++---- 04-call-summaries/run.py | 29 ++++++++------ 3 files changed, 42 insertions(+), 74 deletions(-) diff --git a/04-call-summaries/chunking.py b/04-call-summaries/chunking.py index 03e0812..d875d32 100644 --- a/04-call-summaries/chunking.py +++ b/04-call-summaries/chunking.py @@ -5,7 +5,7 @@ ) from langchain_core.prompts import PromptTemplate from langchain.docstore.document import Document -from llm import google_gemini_client, claude_client, gpt_client, ollama_client +from llm import LLM from run import get_transcript @@ -78,81 +78,46 @@ def chunking_ingest(transcript, prompt): llm = input() or "1" if llm == "2": - client = ollama_client(model_name="dolphin-mistral") + client = LLM(client_name="ollama", model_name="dolphin-mistral") print("""---------- Dolphin """) - for text in text_chunks: - formatted_prompt = prompt_template.format( - text=text.page_content, template=template - ) - print("----------------------") - template = ollama_client(client=client, prompt=formatted_prompt) - return template + elif llm == "3": - gemini = google_gemini_client() + client = LLM(client_name="gemini") print("""---------- Gemini Flash 1.5 """) - for text in text_chunks: - formatted_prompt = prompt_template.format( - text=text.page_content, template=template - ) - print("----------------------") - template = google_gemini_client(client=gemini, prompt=formatted_prompt) - return template elif llm == "4": print("""---------- GPT 4 """) - gpt = gpt_client() - for text in text_chunks: - formatted_prompt = prompt_template.format( - text=text.page_content, template=template - ) - print("----------------------") - template = gpt_client( - client=gpt, model_choice="gpt4", prompt=formatted_prompt - ) - return template + client = LLM(client_name="gpt", model_name="gpt4") elif llm == "5": print("""---------- GPT 4o """) - gpt = gpt_client() - for text in text_chunks: - formatted_prompt = prompt_template.format( - text=text.page_content, template=template - ) - print("----------------------") - template = gpt_client( - client=gpt, model_choice="gpt-4o", prompt=formatted_prompt - ) - return template + client = LLM(client_name="gpt", model_name="gpt-4o") elif llm == "6": print("""---------- Claude 3 """) - claude = claude_client() - for text in text_chunks: - formatted_prompt = prompt_template.format( - text=text.page_content, template=template - ) - print("----------------------") - template = claude_client(client=claude, prompt=formatted_prompt) - return template + client = LLM(client_name="claude") else: print(""" Openhermes """) - ollama = ollama_client(model_name="openhermes") - for text in text_chunks: - formatted_prompt = prompt_template.format( - text=text.page_content, template=template - ) - print("----------------------") - template = ollama_client(client=ollama, prompt=formatted_prompt) - return template + client = LLM(client_name="ollama", model_name="openhermes") + client.get_client() + for text in text_chunks: + formatted_prompt = prompt_template.format( + text=text.page_content, template=template + ) + print("Processing Text Chunk") + template = client.generate_text(prompt=formatted_prompt) + print("Complete") + return template -print(chunking_ingest(transcript=get_transcript(), prompt=CHUNKING_PROMPT)) +if __name__ == "__main__": + print(chunking_ingest(transcript=get_transcript(), prompt=CHUNKING_PROMPT)) diff --git a/04-call-summaries/llm.py b/04-call-summaries/llm.py index 31990f5..9904adb 100644 --- a/04-call-summaries/llm.py +++ b/04-call-summaries/llm.py @@ -30,8 +30,8 @@ def __init__( def get_client(self): """Retrieves the llm client""" if self.client_name == "ollama": - if not settings: - settings = { + if self.settings is None: + self.settings = { # "temperature": 0.1, # "system": "", # "template": "", @@ -47,25 +47,23 @@ def get_client(self): # Get a Google API key by following the steps after clicking on Get an API key button # at https://ai.google.dev/tutorials/setup GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") - print("LLM settings:", self.model_name, settings) - if self.model_version is None: self.model_version = "gemini-1.5-flash-latest" elif self.model_name == "gemini-pro": self.model_version = "gemini-1.5-pro-latest" genai.configure(api_key=GOOGLE_API_KEY) - if settings: + if self.settings is not None: genai.GenerationConfig(**self.settings) self.client = genai.GenerativeModel(self.model_name) elif self.client_name == "gpt": - if self.model_name is None: - self.model_version = "gpt-4o" - elif self.model_name == "gpt3": + if self.model_name == "gpt3": self.model_version = "gpt-3.5-turbo" elif self.model_name == "gpt4": self.model_version = "gpt-4-turbo" + else: + self.model_version = "gpt-4o" # Get API key from https://platform.openai.com/api-keys OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") diff --git a/04-call-summaries/run.py b/04-call-summaries/run.py index ef790ec..65af70d 100644 --- a/04-call-summaries/run.py +++ b/04-call-summaries/run.py @@ -53,14 +53,12 @@ def stuffing_summary(prompt=None): llm = input() or "1" if llm == "2": - ollama = LLM(client_name="ollama", model_name="dolphin-mistral") - response = ollama.generate_text(prompt=prompt) + client = LLM(client_name="ollama", model_name="dolphin-mistral") print("""---------- Dolphin """) elif llm == "3": - gemini = LLM(client_name="gemini") - response = gemini.generate_text(prompt=prompt) + client = LLM(client_name="gemini") print("""---------- Gemini Flash 1.5 """) @@ -68,25 +66,23 @@ def stuffing_summary(prompt=None): print("""---------- GPT 4 """) - gpt = LLM(client_name="gpt", model_name="gpt4") - response = gpt.generate_text(prompt=prompt) + client = LLM(client_name="gpt", model_name="gpt4") elif llm == "5": print("""---------- GPT 4o """) - gpt = LLM(client_name="gpt", model_name="gpt4o") - response = gpt.generate_text(prompt=prompt) + client = LLM(client_name="gpt", model_name="gpt4o") elif llm == "6": print("""---------- Claude 3 """) - claude = LLM(client_name="claude") - response = claude.generate_text(prompt=prompt) + client = LLM(client_name="claude") elif llm == "7": print(""" Openhermes """) ollama_openhermes = LLM(client_name="ollama", model_name="openhermes") + ollama_openhermes.get_client() ollama_openhermes_response = ollama_openhermes.generate_text(prompt=prompt) print(ollama_openhermes_response) @@ -94,6 +90,7 @@ def stuffing_summary(prompt=None): Dolphin """) ollama_dolphin = LLM(client_name="ollama", model_name="dolphin-mistral") + ollama_dolphin.get_client() dolphin_response = ollama_dolphin.generate_text(prompt=prompt) print(dolphin_response) @@ -101,6 +98,7 @@ def stuffing_summary(prompt=None): Gemini Flash 1.5 """) gemini = LLM(client_name="gemini") + gemini.get_client() gemini_response = gemini.generate_text(prompt=prompt) print(gemini_response) @@ -108,6 +106,7 @@ def stuffing_summary(prompt=None): GPT 4 """) gpt_4 = LLM(client_name="gpt", model_name="gpt4") + gpt_4.get_client() gpt_4_response = gpt_4.generate_text(prompt=prompt) print(gpt_4_response) @@ -115,6 +114,7 @@ def stuffing_summary(prompt=None): GPT 4o """) gpt_4o = LLM(client_name="_4o", model_name="gpt4o") + gpt_4o.get_client() gpt_4o_response = gpt_4o.generate_text(prompt=prompt) print(gpt_4o_response) @@ -122,13 +122,18 @@ def stuffing_summary(prompt=None): Claude 3 """) claude = LLM(client_name="claude") + claude.get_client() claude_response = claude.generate_text(prompt=prompt) print(claude_response) else: - ollama = LLM(client_name="ollama", model_name="openhermes") - response = ollama.generate_text(prompt=prompt) + client = LLM(client_name="ollama", model_name="openhermes") print(""" Openhermes """) + client.get_client() + response = client.generate_text(prompt=prompt) if response: print(response) + +if __name__ == "__main__": + stuffing_summary(prompt=formatted_prompt) From 920ab0a55dd3ffcafbc64edb285f8212b7946f13 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Tue, 21 May 2024 12:51:48 -0400 Subject: [PATCH 11/13] formatted --- 04-call-summaries/chunking.py | 1 + 04-call-summaries/run.py | 1 + 2 files changed, 2 insertions(+) diff --git a/04-call-summaries/chunking.py b/04-call-summaries/chunking.py index d875d32..f3c7433 100644 --- a/04-call-summaries/chunking.py +++ b/04-call-summaries/chunking.py @@ -119,5 +119,6 @@ def chunking_ingest(transcript, prompt): print("Complete") return template + if __name__ == "__main__": print(chunking_ingest(transcript=get_transcript(), prompt=CHUNKING_PROMPT)) diff --git a/04-call-summaries/run.py b/04-call-summaries/run.py index 65af70d..7500164 100644 --- a/04-call-summaries/run.py +++ b/04-call-summaries/run.py @@ -135,5 +135,6 @@ def stuffing_summary(prompt=None): if response: print(response) + if __name__ == "__main__": stuffing_summary(prompt=formatted_prompt) From 89e7171e1e53f3dbaa8af4bd2860816318528568 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Wed, 22 May 2024 10:57:24 -0400 Subject: [PATCH 12/13] fixes to gemini model default --- 04-call-summaries/llm.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/04-call-summaries/llm.py b/04-call-summaries/llm.py index 9904adb..b092fcd 100644 --- a/04-call-summaries/llm.py +++ b/04-call-summaries/llm.py @@ -47,15 +47,15 @@ def get_client(self): # Get a Google API key by following the steps after clicking on Get an API key button # at https://ai.google.dev/tutorials/setup GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY") - if self.model_version is None: - self.model_version = "gemini-1.5-flash-latest" - elif self.model_name == "gemini-pro": + if self.model_name == "gemini-pro": self.model_version = "gemini-1.5-pro-latest" + else: + self.model_version = "gemini-1.5-flash-latest" genai.configure(api_key=GOOGLE_API_KEY) if self.settings is not None: genai.GenerationConfig(**self.settings) - self.client = genai.GenerativeModel(self.model_name) + self.client = genai.GenerativeModel(self.model_version) elif self.client_name == "gpt": if self.model_name == "gpt3": @@ -81,7 +81,7 @@ def generate_text(self, prompt=None): if self.client_name == "ollama": return self.client.invoke(prompt) elif self.client_name == "gemini": - return self.client.generate(prompt).text + return self.client.generate_content(prompt).text elif self.client_name == "gpt": return ( self.client.chat.completions.create( From f5b604495df23b3bcbfbbb77252ecc10636a1353 Mon Sep 17 00:00:00 2001 From: C Cheng Date: Wed, 22 May 2024 10:59:06 -0400 Subject: [PATCH 13/13] rename function call --- 04-call-summaries/chunking.py | 18 ++++++++++++++---- 04-call-summaries/llm.py | 2 +- 04-call-summaries/run.py | 14 +++++++------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/04-call-summaries/chunking.py b/04-call-summaries/chunking.py index f3c7433..5aa10bf 100644 --- a/04-call-summaries/chunking.py +++ b/04-call-summaries/chunking.py @@ -1,3 +1,4 @@ +import datetime from langchain_text_splitters import ( RecursiveCharacterTextSplitter, NLTKTextSplitter, @@ -34,8 +35,8 @@ def get_text_chunks(text, chunk_size, chunk_overlap, text_splitter_choice): CHUNKING_PROMPT = """ You are a helpful AI assistant tasked with summarizing transcripts, however we can only process the transcripts in pieces. -Fill out the fields with the text given {text}. If the following template already has the field filled out, do not overwrite this information. -Please fill out the data with the following template: {template} +Please fill out and return the following template: {template} with data in the text: {text} +If the following template already has the field filled out, do not overwrite this information. """ initial_temp = """ @@ -109,7 +110,9 @@ def chunking_ingest(transcript, prompt): """) client = LLM(client_name="ollama", model_name="openhermes") - client.get_client() + client.init_client() + ct = datetime.datetime.now() + print("current time:-", ct) for text in text_chunks: formatted_prompt = prompt_template.format( text=text.page_content, template=template @@ -121,4 +124,11 @@ def chunking_ingest(transcript, prompt): if __name__ == "__main__": - print(chunking_ingest(transcript=get_transcript(), prompt=CHUNKING_PROMPT)) + print( + chunking_ingest( + transcript=get_transcript("./son_calling_behalf_mother_transcript.txt"), + prompt=CHUNKING_PROMPT, + ) + ) + ct = datetime.datetime.now() + print(ct) diff --git a/04-call-summaries/llm.py b/04-call-summaries/llm.py index b092fcd..b9e68c9 100644 --- a/04-call-summaries/llm.py +++ b/04-call-summaries/llm.py @@ -27,7 +27,7 @@ def __init__( self.client = None self.settings = settings - def get_client(self): + def init_client(self): """Retrieves the llm client""" if self.client_name == "ollama": if self.settings is None: diff --git a/04-call-summaries/run.py b/04-call-summaries/run.py index 7500164..b271d7b 100644 --- a/04-call-summaries/run.py +++ b/04-call-summaries/run.py @@ -82,7 +82,7 @@ def stuffing_summary(prompt=None): Openhermes """) ollama_openhermes = LLM(client_name="ollama", model_name="openhermes") - ollama_openhermes.get_client() + ollama_openhermes.init_client() ollama_openhermes_response = ollama_openhermes.generate_text(prompt=prompt) print(ollama_openhermes_response) @@ -90,7 +90,7 @@ def stuffing_summary(prompt=None): Dolphin """) ollama_dolphin = LLM(client_name="ollama", model_name="dolphin-mistral") - ollama_dolphin.get_client() + ollama_dolphin.init_client() dolphin_response = ollama_dolphin.generate_text(prompt=prompt) print(dolphin_response) @@ -98,7 +98,7 @@ def stuffing_summary(prompt=None): Gemini Flash 1.5 """) gemini = LLM(client_name="gemini") - gemini.get_client() + gemini.init_client() gemini_response = gemini.generate_text(prompt=prompt) print(gemini_response) @@ -106,7 +106,7 @@ def stuffing_summary(prompt=None): GPT 4 """) gpt_4 = LLM(client_name="gpt", model_name="gpt4") - gpt_4.get_client() + gpt_4.init_client() gpt_4_response = gpt_4.generate_text(prompt=prompt) print(gpt_4_response) @@ -114,7 +114,7 @@ def stuffing_summary(prompt=None): GPT 4o """) gpt_4o = LLM(client_name="_4o", model_name="gpt4o") - gpt_4o.get_client() + gpt_4o.init_client() gpt_4o_response = gpt_4o.generate_text(prompt=prompt) print(gpt_4o_response) @@ -122,7 +122,7 @@ def stuffing_summary(prompt=None): Claude 3 """) claude = LLM(client_name="claude") - claude.get_client() + claude.init_client() claude_response = claude.generate_text(prompt=prompt) print(claude_response) else: @@ -130,7 +130,7 @@ def stuffing_summary(prompt=None): print(""" Openhermes """) - client.get_client() + client.init_client() response = client.generate_text(prompt=prompt) if response: print(response)