Editing
Long Context and Memory in LLMs
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== <span style="color: #FFFFFF;">Applying</span> == '''Long-context document QA with sliding summary memory:''' <syntaxhighlight lang="python"> from openai import OpenAI from anthropic import Anthropic import tiktoken client = OpenAI() enc = tiktoken.encoding_for_model("gpt-4o") def chunk_document(text: str, chunk_size: int = 8000, overlap: int = 500) -> list[str]: """Split document into overlapping chunks.""" tokens = enc.encode(text) chunks = [] for i in range(0, len(tokens), chunk_size - overlap): chunk_tokens = tokens[i:i + chunk_size] chunks.append(enc.decode(chunk_tokens)) return chunks def answer_with_long_context(document: str, question: str) -> str: chunks = chunk_document(document) # Map phase: extract relevant info from each chunk relevant_excerpts = [] for chunk in chunks: response = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role":"user", "content": f"From the following text, extract any information relevant to: '{question}'\nText: {chunk}\nIf nothing relevant, respond 'NONE'."}] ) excerpt = response.choices[0].message.content if excerpt != "NONE": relevant_excerpts.append(excerpt) # Reduce phase: synthesize extracted excerpts into final answer combined = "\n\n".join(relevant_excerpts) final = client.chat.completions.create( model="gpt-4o", messages=[{"role":"user", "content": f"Based on these excerpts, answer: {question}\n\nExcerpts:\n{combined}"}] ) return final.choices[0].message.content </syntaxhighlight> ; Long context strategy by use case : '''Full book comprehension''' β Gemini 1.5 Pro (1M tokens), Claude 3.5 Sonnet (200k) : '''Long-document QA''' β RAG with chunking + cross-encoder reranker : '''Multi-session agent memory''' β Conversation summary + vector DB (episodic memory) : '''Code repository understanding''' β Tree-sitter parsing + selective context, CodeGraph : '''Long conversations''' β Progressive summarization of older turns into rolling summary </div> <div style="background-color: #8B4500; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;">
Summary:
Please note that all contributions to BloomWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BloomWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information