Commit ·
5a95d6e
1
Parent(s): cc69e31
update: update tools call function
Browse files- .env +1 -0
- .gitignore +1 -0
- app.py +70 -37
- chatbot_agent.py +3 -8
- requirements.txt +6 -2
- tools.py +57 -20
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
OPENAI_API_KEY = ''
|
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
app.py
CHANGED
|
@@ -1,39 +1,72 @@
|
|
| 1 |
-
import gradio
|
| 2 |
-
from
|
| 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 |
if __name__ == "__main__":
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio
|
| 2 |
+
from chatbot_agent import *
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 8 |
+
# from huggingface_hub import InferenceClient
|
| 9 |
+
# """
|
| 10 |
+
# For more information on `huggingface_hub` Inference API support, please check the docs:
|
| 11 |
+
# https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 12 |
+
# """
|
| 13 |
+
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# def respond(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,top_p,):
|
| 17 |
+
# messages = [{"role": "system", "content": system_message}]
|
| 18 |
+
# for val in history:
|
| 19 |
+
# if val[0]:
|
| 20 |
+
# messages.append({"role": "user", "content": val[0]})
|
| 21 |
+
# if val[1]:
|
| 22 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
| 23 |
+
# messages.append({"role": "user", "content": message})
|
| 24 |
+
# response = ""
|
| 25 |
+
# for message in client.chat_completion(messages,max_tokens=max_tokens,stream=True,
|
| 26 |
+
# temperature=temperature,top_p=top_p,):
|
| 27 |
+
# token = message.choices[0].delta.content
|
| 28 |
+
# response += token
|
| 29 |
+
# yield response
|
| 30 |
+
|
| 31 |
+
# """
|
| 32 |
+
# For information on how to customize the ChatInterface, peruse the gradio docs:
|
| 33 |
+
# https://www.gradio.app/docs/chatinterface
|
| 34 |
+
# """
|
| 35 |
+
# demo = gr.ChatInterface(
|
| 36 |
+
# respond,
|
| 37 |
+
# additional_inputs=[
|
| 38 |
+
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 39 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 40 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 41 |
+
# gr.Slider(minimum=0.1,maximum=1.0,value=0.95,step=0.05,label="Top-p (nucleus sampling)",),
|
| 42 |
+
# ],
|
| 43 |
+
# )
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def message_and_history(input, history):
|
| 47 |
+
history = history or []
|
| 48 |
+
s = list(sum(history, ()))
|
| 49 |
+
s.append(input)
|
| 50 |
+
inp = ' '.join(s)
|
| 51 |
+
output = chatbot_llm.ask_chatbot(user_message = inp)
|
| 52 |
+
print(output)
|
| 53 |
+
history.append((input, output))
|
| 54 |
+
return history, history
|
| 55 |
+
|
| 56 |
+
block = gradio.Blocks(theme=gradio.themes.Monochrome())
|
| 57 |
+
chatbot_llm = ChatbotAgent()
|
| 58 |
+
chatbot_llm.create_user(user_id="test")
|
| 59 |
+
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
+
with block:
|
| 63 |
+
gradio.Markdown("""<h1><center>ChatBot
|
| 64 |
+
ChatBot for suggesting Cost and video script</center></h1>""")
|
| 65 |
+
chatbot = gradio.Chatbot()
|
| 66 |
+
message = gradio.Textbox(placeholder="Enter Your Query Here")
|
| 67 |
+
state = gradio.State()
|
| 68 |
+
submit = gradio.Button("SEND")
|
| 69 |
+
submit.click(message_and_history,
|
| 70 |
+
inputs=[message, state],
|
| 71 |
+
outputs=[chatbot, state])
|
| 72 |
+
block.launch(debug = True)
|
chatbot_agent.py
CHANGED
|
@@ -1,17 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
| 3 |
-
from langchain.schema import Document
|
| 4 |
-
from langchain_core.prompts import PromptTemplate
|
| 5 |
-
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 6 |
-
from langchain.vectorstores import FAISS
|
| 7 |
-
from langchain_core.runnables import RunnablePassthrough
|
| 8 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 9 |
-
import pandas as pd
|
| 10 |
from abc import ABC, abstractmethod
|
| 11 |
from tools import *
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
| 16 |
|
| 17 |
|
|
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from abc import ABC, abstractmethod
|
| 4 |
from tools import *
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
|
| 8 |
+
load_dotenv()
|
| 9 |
+
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 10 |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
| 11 |
|
| 12 |
|
requirements.txt
CHANGED
|
@@ -2,7 +2,11 @@ huggingface_hub==0.25.2
|
|
| 2 |
autogluon
|
| 3 |
scikit-learn
|
| 4 |
seaborn
|
| 5 |
-
langchain-community
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
faiss-cpu
|
| 7 |
openai
|
| 8 |
-
|
|
|
|
| 2 |
autogluon
|
| 3 |
scikit-learn
|
| 4 |
seaborn
|
| 5 |
+
langchain-community
|
| 6 |
+
langchain-openai
|
| 7 |
+
langchain_core
|
| 8 |
+
langchain-google-vertexai
|
| 9 |
+
langchain
|
| 10 |
faiss-cpu
|
| 11 |
openai
|
| 12 |
+
gradio
|
tools.py
CHANGED
|
@@ -1,22 +1,26 @@
|
|
| 1 |
import os
|
| 2 |
-
from openai import OpenAI
|
| 3 |
from langchain.schema import Document
|
| 4 |
from langchain_core.prompts import PromptTemplate
|
| 5 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 6 |
-
from
|
| 7 |
from langchain_core.runnables import RunnablePassthrough
|
| 8 |
from langchain_core.output_parsers import StrOutputParser
|
|
|
|
|
|
|
| 9 |
import pandas as pd
|
|
|
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# Function to convert a DataFrame row to a Document
|
| 12 |
def row_to_document(row):
|
| 13 |
content = (
|
| 14 |
-
f"Name of KOL: {row['
|
| 15 |
f"received {row['LIKES']} likes, "
|
| 16 |
f"average {row['Avg VIEWS from last 5 videos']} views from the last 5 videos, "
|
| 17 |
f"and costs ${row['COST']} per collaboration.")
|
| 18 |
metadata = {
|
| 19 |
-
'KOL Name': row['
|
| 20 |
'FOLLOWERS': row['FOLLOWERS'],
|
| 21 |
'LIKES': row['LIKES'],
|
| 22 |
'Avg VIEWS from last 5 videos': row['Avg VIEWS from last 5 videos'],
|
|
@@ -31,18 +35,29 @@ def call_rag_workflow(prompts:str = "") -> str:
|
|
| 31 |
chain = ({"information": retriever, "prompt": RunnablePassthrough()} | prompt | model | StrOutputParser())
|
| 32 |
response = chain.invoke(prompts)
|
| 33 |
return f"respone from chatbot using RAG: {str(response)}"
|
| 34 |
-
def inference_cost_model(
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def get_tools():
|
| 37 |
-
tools = [call_rag_workflow
|
|
|
|
|
|
|
|
|
|
| 38 |
tools = {func.__name__: func for func in tools}
|
| 39 |
return tools
|
| 40 |
|
| 41 |
|
| 42 |
data_lineup = pd.read_csv("data/data_lineup.csv")
|
|
|
|
| 43 |
documents = data_lineup.apply(row_to_document, axis=1).tolist()
|
| 44 |
|
| 45 |
-
|
| 46 |
openai_tools = [
|
| 47 |
{
|
| 48 |
"type": "function",
|
|
@@ -56,17 +71,39 @@ openai_tools = [
|
|
| 56 |
to perform query to get knowdlege from vector store. Default: None"""},},
|
| 57 |
"required": ["prompts"]},
|
| 58 |
},
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
]
|
| 72 |
local_tools = get_tools()
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from langchain.schema import Document
|
| 3 |
from langchain_core.prompts import PromptTemplate
|
| 4 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 5 |
+
from langchain_community.vectorstores import FAISS
|
| 6 |
from langchain_core.runnables import RunnablePassthrough
|
| 7 |
from langchain_core.output_parsers import StrOutputParser
|
| 8 |
+
from autogluon.tabular import TabularPredictor
|
| 9 |
+
import random
|
| 10 |
import pandas as pd
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
|
| 13 |
+
load_dotenv()
|
| 14 |
+
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 15 |
# Function to convert a DataFrame row to a Document
|
| 16 |
def row_to_document(row):
|
| 17 |
content = (
|
| 18 |
+
f"Name of KOL: {row['kol_name']} has {row['FOLLOWERS']} followers, "
|
| 19 |
f"received {row['LIKES']} likes, "
|
| 20 |
f"average {row['Avg VIEWS from last 5 videos']} views from the last 5 videos, "
|
| 21 |
f"and costs ${row['COST']} per collaboration.")
|
| 22 |
metadata = {
|
| 23 |
+
'KOL Name': row['kol_name'],
|
| 24 |
'FOLLOWERS': row['FOLLOWERS'],
|
| 25 |
'LIKES': row['LIKES'],
|
| 26 |
'Avg VIEWS from last 5 videos': row['Avg VIEWS from last 5 videos'],
|
|
|
|
| 35 |
chain = ({"information": retriever, "prompt": RunnablePassthrough()} | prompt | model | StrOutputParser())
|
| 36 |
response = chain.invoke(prompts)
|
| 37 |
return f"respone from chatbot using RAG: {str(response)}"
|
| 38 |
+
def inference_cost_model(prompts: str = "") -> str:
|
| 39 |
+
model = TabularPredictor.load("AutogluonModels/ag-20241226_034850")
|
| 40 |
+
result = model.predict(prompts)
|
| 41 |
+
return f"respone from chatbot using cost prediction model: {str(result)}"
|
| 42 |
+
def inference_view_model(prompts: str = "") -> str:
|
| 43 |
+
model = TabularPredictor.load("AutogluonModels/ag-20241227_060803")
|
| 44 |
+
result = model.predict(prompts)
|
| 45 |
+
return f"respone from chatbot using cost prediction model: {str(result)}"
|
| 46 |
+
def inference_ranking_model(prompts: str = "") -> str:
|
| 47 |
+
return f"respone from chatbot using cost prediction model: {str(random.choice([1,2,3,4,5]))}"
|
| 48 |
def get_tools():
|
| 49 |
+
tools = [call_rag_workflow,
|
| 50 |
+
inference_cost_model,
|
| 51 |
+
inference_view_model,
|
| 52 |
+
inference_ranking_model]
|
| 53 |
tools = {func.__name__: func for func in tools}
|
| 54 |
return tools
|
| 55 |
|
| 56 |
|
| 57 |
data_lineup = pd.read_csv("data/data_lineup.csv")
|
| 58 |
+
data_lineup.rename(columns = {"KOL Name" : "kol_name"}, inplace=True)
|
| 59 |
documents = data_lineup.apply(row_to_document, axis=1).tolist()
|
| 60 |
|
|
|
|
| 61 |
openai_tools = [
|
| 62 |
{
|
| 63 |
"type": "function",
|
|
|
|
| 71 |
to perform query to get knowdlege from vector store. Default: None"""},},
|
| 72 |
"required": ["prompts"]},
|
| 73 |
},
|
| 74 |
+
}
|
| 75 |
+
,{
|
| 76 |
+
"type": "function",
|
| 77 |
+
"function": {
|
| 78 |
+
"name": "inference_cost_model",
|
| 79 |
+
"description": """this is a description""",
|
| 80 |
+
"parameters": {"type": "object",
|
| 81 |
+
"properties": {
|
| 82 |
+
"prompts": {"type": "string", "description": """this is a description"""},},
|
| 83 |
+
"required": ["prompts"]},
|
| 84 |
+
},
|
| 85 |
+
}
|
| 86 |
+
,{
|
| 87 |
+
"type": "function",
|
| 88 |
+
"function": {
|
| 89 |
+
"name": "inference_view_model",
|
| 90 |
+
"description": """this is a description""",
|
| 91 |
+
"parameters": {"type": "object",
|
| 92 |
+
"properties": {
|
| 93 |
+
"prompts": {"type": "string", "description": """this is a description"""},},
|
| 94 |
+
"required": ["prompts"]},
|
| 95 |
+
},
|
| 96 |
+
}
|
| 97 |
+
,{
|
| 98 |
+
"type": "function",
|
| 99 |
+
"function": {
|
| 100 |
+
"name": "inference_ranking_model",
|
| 101 |
+
"description": """this is a description""",
|
| 102 |
+
"parameters": {"type": "object",
|
| 103 |
+
"properties": {
|
| 104 |
+
"prompts": {"type": "string", "description": """this is a description"""},},
|
| 105 |
+
"required": ["prompts"]},
|
| 106 |
+
},
|
| 107 |
+
}
|
| 108 |
]
|
| 109 |
local_tools = get_tools()
|