-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DST-199: base code for chunking #24
Merged
+324
−147
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
eec0438
replace 3.5 with 4o and use gemini flash 1.5
fa8961b
format
61b4fd0
set up chunking function
6b34a5b
refactor to not instantiate with every message call
6930520
create function for stuffing code
93af4ab
implement chunking code
11e9db1
fix instantiation err
26a079a
refactor llm choices into class
94fbc1b
update stuffing code
460fc09
get client call for responses
920ab0a
formatted
89e7171
fixes to gemini model default
f5b6044
rename function call
14b9f68
Merge branch 'main' of github.com:navapbc/labs-gen-ai-experiments int…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,158 @@ | ||
from langchain_text_splitters import ( | ||
RecursiveCharacterTextSplitter, | ||
NLTKTextSplitter, | ||
SpacyTextSplitter, | ||
) | ||
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 run import get_transcript | ||
|
||
|
||
# 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 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 | ||
- 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_chunks = get_text_chunks( | ||
transcript, chunk_size=750, chunk_overlap=300, text_splitter_choice="2" | ||
) | ||
prompt_template = PromptTemplate.from_template(prompt) | ||
template = initial_temp | ||
ccheng26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
ccheng26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
""") | ||
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 | ||
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(chunking_ingest(transcript=get_transcript(), prompt=CHUNKING_PROMPT)) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the current code always uses
"2"
, so the chunk_size and chunk_overlap are not used.