Spaces:
Sleeping
Sleeping
Create chat_agent.py
Browse files- chat_agent.py +56 -0
chat_agent.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.agents import Tool
|
2 |
+
from langchain.memory import ConversationBufferMemory
|
3 |
+
from langchain.chat_models import ChatOpenAI
|
4 |
+
from langchain.agents import initialize_agent
|
5 |
+
from llama_index import GPTSimpleVectorIndex
|
6 |
+
import os
|
7 |
+
from langchain.schema import (
|
8 |
+
SystemMessage
|
9 |
+
)
|
10 |
+
#import openai
|
11 |
+
os.environ["OPENAI_API_KEY"] = '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'
|
12 |
+
#openai.organization = "org-LWOdWb7zopsw1g1tJUgDOYUo"
|
13 |
+
#openai.api_key = "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"
|
14 |
+
|
15 |
+
|
16 |
+
class ChatBot:
|
17 |
+
def __init__(self, memory, agent_chain):
|
18 |
+
self.memory = memory
|
19 |
+
self.agent = agent_chain
|
20 |
+
|
21 |
+
|
22 |
+
def create_chatbot(model_name, seed_memory=None):
|
23 |
+
# search = GoogleSearchAPIWrapper()
|
24 |
+
# tools = [
|
25 |
+
# Tool(
|
26 |
+
# name="Current Search",
|
27 |
+
# func=search.run,
|
28 |
+
# description="useful for all question that asks about live events",
|
29 |
+
# ),
|
30 |
+
# Tool(
|
31 |
+
# name="Topic Search",
|
32 |
+
# func=search.run,
|
33 |
+
# description="useful for all question that are related to a particular topic, product, concept, or service",
|
34 |
+
# )
|
35 |
+
# ]
|
36 |
+
index = GPTSimpleVectorIndex.load_from_disk('martin.json')
|
37 |
+
query_mode ="svm"
|
38 |
+
|
39 |
+
tools = [
|
40 |
+
Tool(
|
41 |
+
name="GPT Index",
|
42 |
+
func=lambda q: str(index.query(q,vector_store_query_mode=query_mode)),
|
43 |
+
description="useful for when you want to answer questions about Martin Seligman , personal issues , family issues , working issues , children issues , positive psychonogy related , such as Broaden-and-Build Theory,PERMA Model,Positive Psychology Interventions,Mindfulness,Meaning and Purpose,Positive Relationships,Resilience,Flow,Optimism,Character Strengths,Happiness and Subjective Well-Being. The input to this tool should be a complete english sentence.",
|
44 |
+
return_direct=True
|
45 |
+
),
|
46 |
+
]
|
47 |
+
|
48 |
+
# messages = [
|
49 |
+
# SystemMessage(content="You are Martin Seligman. You use a tone that is warm and kind.")
|
50 |
+
# ]
|
51 |
+
|
52 |
+
memory = seed_memory if seed_memory is not None else ConversationBufferMemory(memory_key="chat_history")
|
53 |
+
chat = ChatOpenAI(temperature=0, model_name=model_name)
|
54 |
+
agent_chain = initialize_agent(tools, chat, agent="conversational-react-description", verbose=True, memory=memory)
|
55 |
+
|
56 |
+
return ChatBot(memory, agent_chain)
|