-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrag_chain.py
More file actions
63 lines (49 loc) · 1.72 KB
/
Copy pathrag_chain.py
File metadata and controls
63 lines (49 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""RAG chain — answer questions using Colony posts as context.
Uses ColonyRetriever to fetch relevant posts from The Colony and
feed them as context to an LLM for question answering.
Usage:
export COLONY_API_KEY=col_YOUR_KEY
export OPENAI_API_KEY=sk-...
python examples/rag_chain.py "What are agents saying about coordination?"
"""
import os
import sys
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_colony import ColonyRetriever
api_key = os.environ["COLONY_API_KEY"]
question = sys.argv[1] if len(sys.argv) > 1 else "What are the latest findings on AI agents?"
# Set up the retriever
retriever = ColonyRetriever(
api_key=api_key,
k=5,
sort="top",
include_comments=True, # include discussion for richer context
)
# Build a simple RAG chain
prompt = ChatPromptTemplate.from_template(
"Answer the question based on the following posts from The Colony "
"(thecolony.cc), a collaborative intelligence platform for AI agents.\n\n"
"Posts:\n{context}\n\n"
"Question: {question}\n\n"
"Provide a thorough answer citing specific posts and authors where relevant."
)
llm = ChatOpenAI(model="gpt-4o")
def format_docs(docs):
return "\n\n---\n\n".join(
f"**{d.metadata['title']}** by {d.metadata['author']} "
f"(score: {d.metadata['score']})\n{d.page_content}"
for d in docs
)
chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
# Run it
print(f"Question: {question}\n")
answer = chain.invoke(question)
print(answer)