Spaces:
Sleeping
Sleeping
File size: 2,059 Bytes
c30b9ce 610fc84 c30b9ce 1000895 c30b9ce 610fc84 c30b9ce e284e34 610fc84 c30b9ce |
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 |
from langchain.agents import Tool
from langchain.memory import ConversationBufferMemory ,ConversationBufferWindowMemory
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent
from llama_index import GPTSimpleVectorIndex
import os
from langchain.schema import (
SystemMessage
)
class ChatBot:
def __init__(self, memory, agent_chain):
self.memory = memory
self.agent = agent_chain
def create_chatbot(model_name, seed_memory=None):
# search = GoogleSearchAPIWrapper()
# tools = [
# Tool(
# name="Current Search",
# func=search.run,
# description="useful for all question that asks about live events",
# ),
# Tool(
# name="Topic Search",
# func=search.run,
# description="useful for all question that are related to a particular topic, product, concept, or service",
# )
# ]
index = GPTSimpleVectorIndex.load_from_disk('martin.json')
query_mode ="svm"
tools = [
Tool(
name="GPT Index",
func=lambda q: str(index.query(q,vector_store_query_mode=query_mode)),
description="useful for when you want to answer questions about Martin Seligman and positive psychonogy related. The input to this tool should be a complete english sentence.",
return_direct=True
),
]
# messages = [
# SystemMessage(content="You are Martin Seligman. You use a tone that is warm and kind.")
# ]
#memory = ConversationBufferMemory(memory_key="chat_history")
memory = seed_memory if seed_memory is not None else ConversationBufferWindowMemory( k=4 ,memory_key="chat_history")
#memory = seed_memory if seed_memory is not None else ConversationBufferMemory(memory_key="chat_history")
chat = ChatOpenAI(temperature=0, model_name=model_name)
agent_chain = initialize_agent(tools, chat, agent="conversational-react-description", verbose=True, memory=memory)
return ChatBot(memory, agent_chain)
|