-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTxtEmb.py
89 lines (67 loc) · 2.46 KB
/
TxtEmb.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from langchain.agents import AgentExecutor
from langchain_cohere.react_multi_hop.agent import create_cohere_react_agent
from langchain_core.prompts import ChatPromptTemplate
from langchain_cohere.chat_models import ChatCohere
from langchain_community.utilities.sql_database import SQLDatabase
from langchain_community.agent_toolkits import SQLDatabaseToolkit
import os
import json
# load the cohere api key
os.environ["COHERE_API_KEY"] = "D14bT4Bm9SoiXE5ioVryf2DGOyIw1yjm1ccR0giQ"
DB_NAME='Chinook.db'
MODEL="command-r-plus"
llm = ChatCohere(model=MODEL, temperature=0.1,verbose=True)
db = SQLDatabase.from_uri(f"sqlite:///{DB_NAME}")
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
context = toolkit.get_context()
tools = toolkit.get_tools()
print('**List of pre-defined Langchain Tools**')
print([tool.name for tool in tools])
# define the prompt template
prompt = ChatPromptTemplate.from_template("{input}")
# instantiate the ReAct agent
agent = create_cohere_react_agent(
llm=llm,
tools=tools,
prompt=prompt,
)
agent_executor = AgentExecutor(agent=agent,
tools=tools,
verbose=True,
return_intermediate_steps=True
)
output=agent_executor.invoke({
"input": 'what tables are available?',
})
print(output['output'])
output=agent_executor.invoke({
"input": 'show the first row of the Playlist and Genre tables?',
})
### CHATBOT ## Pre trained
# import cohere
# import uuid
# co = cohere.Client("D14bT4Bm9SoiXE5ioVryf2DGOyIw1yjm1ccR0giQ") # Your Cohere API key
# # Create a conversation ID
# conversation_id = str(uuid.uuid4())
# # Define the preamble
# preamble = "You are an expert public speaking coach"
# print('Starting the chat. Type "quit" to end.\n')
# while True:
# # User message
# message = input("User: ")
# # Typing "quit" ends the conversation
# if message.lower() == 'quit':
# print("Ending chat.")
# break
# # Chatbot response
# stream = co.chat_stream(message=message,
# model="command-r-plus",
# preamble=preamble,
# conversation_id=conversation_id)
# print("Chatbot: ", end='')
# for event in stream:
# if event.event_type == "text-generation":
# print(event.text, end='')
# if event.event_type == "stream-end":
# chat_history = event.response.chat_history
# print(f"\n{'-'*100}\n")