but where i can use stored hystory?
- User
but where i can use stored hystory?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
but where i can use stored hystory?
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
How (and where) to feed conversation history into a RAG-style prompt
──────────────────────────────────────────────
What you should keep as “history”
• All user questions and assistant answers that might help the model interpret the NEXT turn (who/what/why).
• Any running “working memory” you build yourself (e.g., a rolling summary).
• IDs or embeddings that let you re-retrieve older turns on demand (to keep the context window small).
Where to put that history in an OpenAI ChatCompletion call
Use the standard messages array. The typical ordering is:
messages = [
# (1) a *persistent* system role
{"role": "system", "content": system_prompt},
# (2) a “history” block – either:
# • the last N raw turns OR
# • a concise summary you maintain
{"role": "assistant", "content": running_summary}, # optional
*recent_turns, # optional
# (3) the fresh retrieval context for **this** question
{"role": "system",
"content": f"🔎 REFERENCE CONTEXT (docs & facts):\n{retrieved_context}\n"
f"Answer only with these facts. If not enough, say so. "
f"Use {dia_lang}."},
# (4) the current user request
{"role": "user", "content": input_text}
]
response = openai.ChatCompletion.create(model="gpt-4o-mini", messages=messages)
Important:
• Keep the history short (or summarized) so the retrieval docs still fit.
• Put the retrieval docs after the history but before the user’s new question; that way the model first sees the conversation thread, then the facts it’s allowed to rely on.
Where to store the history between calls
Option A – In-memory list (easy, but disappears if the process dies).
Option B – Database / key-value store keyed by conversation-ID.
Option C – Vector store (e.g., FAISS, Pinecone). Store embeddings of each past turn; on every new query, retrieve only the k most similar past turns and insert them as the “history” block. This keeps your context window lean and relevant.
Example end-to-end snippet
def build_messages(conv_id: str,
input_text: str,
dia_lang: str,
vector_db,
knowledge_db) -> list[dict]:
# 1) retrieve past turns most related to the new question
past_turns = vector_db.similarity_search(input_text, k=3)
# Each item: {"role": "user"/"assistant", "content": "..."}
# 2) retrieve external docs relevant to the new question
retrieved_context = knowledge_db.rag_retrieve(input_text, k=5)
# 3) optional running summary
running_summary = summarise_turns(past_turns)
# 4) build messages array
system_prompt = (
"You are an assistant that must answer only with the facts provided. "
"If facts are missing, say so."
)
messages = [
{"role": "system", "content": system_prompt},
{"role": "assistant", "content": running_summary},
*past_turns,
{"role": "system",
"content": f"🔎 CONTEXT:\n{retrieved_context}\n"
f"(Answer strictly from this context. Reply in {dia_lang}.)"},
{"role": "user", "content": input_text}
]
return messages
messages to the LLM.That’s “where” the stored history goes: directly into the messages list—either as full past turns or (better) as a concise, retrieval-based slice—before you add the retrieval documents and the current user question.