Spaces:
Runtime error
Runtime error
Create utils/langchain_enhancements.py
Browse files- utils/langchain_enhancements.py +123 -0
utils/langchain_enhancements.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils/langchain_enhancements.py
|
| 2 |
+
|
| 3 |
+
from langchain.memory import ConversationSummaryMemory
|
| 4 |
+
from langchain.agents import Tool, AgentExecutor, OpenAIFunctions
|
| 5 |
+
from langchain.tools import DuckDuckGoSearchRun
|
| 6 |
+
from langchain.retrievers import TimeWeightedVectorStoreRetriever
|
| 7 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 8 |
+
from langchain.vectorstores import Chroma
|
| 9 |
+
from langchain.chat_models import ChatAnthropic
|
| 10 |
+
|
| 11 |
+
class EnhancedLearningSystem:
|
| 12 |
+
def __init__(self, anthropic_api_key):
|
| 13 |
+
self.llm = ChatAnthropic(anthropic_api_key=anthropic_api_key)
|
| 14 |
+
self.memory = self._setup_memory()
|
| 15 |
+
self.tools = self._setup_tools()
|
| 16 |
+
self.agent_executor = self._setup_agent()
|
| 17 |
+
|
| 18 |
+
def _setup_memory(self):
|
| 19 |
+
"""Setup enhanced memory system"""
|
| 20 |
+
# Create vectorstore for storing chat history
|
| 21 |
+
embeddings = OpenAIEmbeddings()
|
| 22 |
+
vectorstore = Chroma(
|
| 23 |
+
collection_name="chat_history",
|
| 24 |
+
embedding_function=embeddings
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Create time-weighted retriever
|
| 28 |
+
retriever = TimeWeightedVectorStoreRetriever(
|
| 29 |
+
vectorstore=vectorstore,
|
| 30 |
+
decay_rate=0.01,
|
| 31 |
+
k=5
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Create summary memory
|
| 35 |
+
memory = ConversationSummaryMemory(
|
| 36 |
+
llm=self.llm,
|
| 37 |
+
memory_key="chat_history",
|
| 38 |
+
return_messages=True
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
return {
|
| 42 |
+
'summary': memory,
|
| 43 |
+
'retriever': retriever
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
def _setup_tools(self):
|
| 47 |
+
"""Setup tools for the learning system"""
|
| 48 |
+
search = DuckDuckGoSearchRun()
|
| 49 |
+
|
| 50 |
+
tools = [
|
| 51 |
+
Tool(
|
| 52 |
+
name="Search",
|
| 53 |
+
func=search.run,
|
| 54 |
+
description="Useful for finding current trading information and examples"
|
| 55 |
+
),
|
| 56 |
+
Tool(
|
| 57 |
+
name="Historical Context",
|
| 58 |
+
func=self._get_historical_context,
|
| 59 |
+
description="Get relevant historical chat context"
|
| 60 |
+
),
|
| 61 |
+
Tool(
|
| 62 |
+
name="Learning Progress",
|
| 63 |
+
func=self._check_learning_progress,
|
| 64 |
+
description="Check user's learning progress and suggest next topics"
|
| 65 |
+
)
|
| 66 |
+
]
|
| 67 |
+
|
| 68 |
+
return tools
|
| 69 |
+
|
| 70 |
+
def _setup_agent(self):
|
| 71 |
+
"""Setup the learning agent"""
|
| 72 |
+
agent = OpenAIFunctions.from_llm_and_tools(
|
| 73 |
+
llm=self.llm,
|
| 74 |
+
tools=self.tools,
|
| 75 |
+
memory=self.memory['summary']
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
return AgentExecutor.from_agent_and_tools(
|
| 79 |
+
agent=agent,
|
| 80 |
+
tools=self.tools,
|
| 81 |
+
memory=self.memory['summary'],
|
| 82 |
+
verbose=True
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
async def _get_historical_context(self, topic):
|
| 86 |
+
"""Retrieve relevant historical context"""
|
| 87 |
+
relevant_docs = await self.memory['retriever'].aget_relevant_documents(topic)
|
| 88 |
+
return [doc.page_content for doc in relevant_docs]
|
| 89 |
+
|
| 90 |
+
def _check_learning_progress(self, topic):
|
| 91 |
+
"""Check user's progress in a topic"""
|
| 92 |
+
# Implement progress tracking logic
|
| 93 |
+
return {
|
| 94 |
+
'mastered_concepts': [],
|
| 95 |
+
'in_progress': [],
|
| 96 |
+
'suggested_next': []
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
async def process_question(self, question):
|
| 100 |
+
"""Process a learning question with enhanced features"""
|
| 101 |
+
# Get historical context
|
| 102 |
+
history = await self._get_historical_context(question)
|
| 103 |
+
|
| 104 |
+
# Execute agent
|
| 105 |
+
response = await self.agent_executor.arun(
|
| 106 |
+
input={
|
| 107 |
+
'question': question,
|
| 108 |
+
'history': history,
|
| 109 |
+
'progress': self._check_learning_progress(question)
|
| 110 |
+
}
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Update memory
|
| 114 |
+
self.memory['summary'].save_context(
|
| 115 |
+
{'input': question},
|
| 116 |
+
{'output': response}
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
return {
|
| 120 |
+
'response': response,
|
| 121 |
+
'context': history,
|
| 122 |
+
'progress': self._check_learning_progress(question)
|
| 123 |
+
}
|