Spaces:
Sleeping
Sleeping
Typing fixes, code cleanup
Browse files- app/app.py +28 -23
- app/langchain_agent.py +15 -70
app/app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import uuid
|
|
|
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
from dotenv import load_dotenv
|
|
@@ -9,38 +10,42 @@ from app.utils import timing
|
|
| 9 |
|
| 10 |
|
| 11 |
@timing
|
| 12 |
-
def predict_interface(message: str, history=None, user_id=
|
| 13 |
-
response = llm_inference(message,
|
| 14 |
return response["output"]
|
| 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 |
def main():
|
| 41 |
logger = get_logger()
|
| 42 |
load_dotenv(override=True, verbose=True, dotenv_path=".env")
|
| 43 |
logger.info("Running chat")
|
|
|
|
| 44 |
chat.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 45 |
|
| 46 |
|
|
|
|
| 1 |
import uuid
|
| 2 |
+
from typing import Any
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
from dotenv import load_dotenv
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
@timing
|
| 13 |
+
def predict_interface(message: str, history: Any = None, user_id: str = "user") -> str:
|
| 14 |
+
response = llm_inference(message, user_id)
|
| 15 |
return response["output"]
|
| 16 |
|
| 17 |
|
| 18 |
+
def create_chat() -> gr.ChatInterface:
|
| 19 |
+
session_id = gr.Textbox(value=str(uuid.uuid4()), type="text", label="session_id")
|
| 20 |
+
example_sentences = [
|
| 21 |
+
"Recommend me something in Quentin Tarantino reggae style",
|
| 22 |
+
"Give me songs with calm and relaxing vibes",
|
| 23 |
+
"I want to listen to something like the movie Inception",
|
| 24 |
+
"I want music that sounds like Lebron James eating soup",
|
| 25 |
+
]
|
| 26 |
+
examples = [[example, f"user_{i}"] for i, example in enumerate(example_sentences)]
|
| 27 |
+
|
| 28 |
+
return gr.ChatInterface(
|
| 29 |
+
predict_interface,
|
| 30 |
+
additional_inputs=[session_id],
|
| 31 |
+
chatbot=gr.Chatbot(height=600),
|
| 32 |
+
textbox=gr.Textbox(
|
| 33 |
+
placeholder="Ask me for music recommendations!", container=False, scale=7
|
| 34 |
+
),
|
| 35 |
+
description="This AI makes song recommendations based on your music style.",
|
| 36 |
+
examples=examples,
|
| 37 |
+
title="Persona Music song recommender",
|
| 38 |
+
retry_btn="Retry",
|
| 39 |
+
clear_btn="Clear",
|
| 40 |
+
undo_btn=None,
|
| 41 |
+
)
|
| 42 |
|
| 43 |
|
| 44 |
def main():
|
| 45 |
logger = get_logger()
|
| 46 |
load_dotenv(override=True, verbose=True, dotenv_path=".env")
|
| 47 |
logger.info("Running chat")
|
| 48 |
+
chat = create_chat()
|
| 49 |
chat.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|
| 50 |
|
| 51 |
|
app/langchain_agent.py
CHANGED
|
@@ -1,33 +1,30 @@
|
|
| 1 |
import os
|
| 2 |
-
import pprint
|
| 3 |
from collections import defaultdict
|
| 4 |
from operator import itemgetter
|
| 5 |
from typing import Any, Dict, List
|
| 6 |
|
| 7 |
-
from langchain.agents import AgentExecutor
|
| 8 |
from langchain.agents.format_scratchpad import format_to_openai_functions
|
| 9 |
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
|
| 10 |
from langchain.callbacks import get_openai_callback
|
| 11 |
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
| 12 |
from langchain.chat_models import ChatOpenAI
|
| 13 |
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 14 |
-
from langchain.schema import
|
| 15 |
-
from
|
| 16 |
-
from langchain.tools.render import format_tool_to_openai_function
|
| 17 |
-
from langfuse.callback import CallbackHandler
|
| 18 |
|
| 19 |
from app.cyanite import free_text_search
|
| 20 |
from app.logger import get_logger
|
| 21 |
from app.utils import timing
|
| 22 |
|
| 23 |
-
system_message = """You are an agent which recommends songs based on music styles provided by the user.
|
| 24 |
-
- A music style could be a combination of instruments, genres or sounds.
|
| 25 |
-
- Use get_music_style_description to generate a description of the user's music style.
|
| 26 |
-
- The styles might contain pop-culture references (artists, movies, TV-Shows, etc) You should include them when generating descriptions.
|
| 27 |
-
- Comment on the description of the style and wish the user to enjoy the recommended songs (he will have received them).
|
| 28 |
-
- Do not mention any songs or artists, nor give a list of songs.
|
| 29 |
-
Write short responses with a respectful and friendly tone.
|
| 30 |
-
"""
|
| 31 |
|
| 32 |
|
| 33 |
describe_music_style_message = """You receive a music style and your goal is to describe it further with genres, instruments and sounds.
|
|
@@ -42,8 +39,7 @@ MEMORY_KEY = "history"
|
|
| 42 |
|
| 43 |
class Globals:
|
| 44 |
llm_with_tools: Any = None
|
| 45 |
-
handlers: List[
|
| 46 |
-
music_styles_to_tracks: Dict[str, List[Any]] = {}
|
| 47 |
conversation_costs: Dict[str, float] = defaultdict(float)
|
| 48 |
|
| 49 |
|
|
@@ -56,7 +52,7 @@ prompt = ChatPromptTemplate.from_messages(
|
|
| 56 |
]
|
| 57 |
)
|
| 58 |
|
| 59 |
-
conversation_memories = defaultdict(
|
| 60 |
lambda: ConversationBufferWindowMemory(
|
| 61 |
memory_key=MEMORY_KEY, return_messages=True, output_key="output", k=4
|
| 62 |
)
|
|
@@ -65,41 +61,6 @@ conversation_memories = defaultdict(
|
|
| 65 |
# global dicts to store the tracks and the conversation costs
|
| 66 |
|
| 67 |
|
| 68 |
-
@tool
|
| 69 |
-
@timing
|
| 70 |
-
def get_music_style_description(music_style: str) -> str:
|
| 71 |
-
"A tool which describes a music style and returns a description of it"
|
| 72 |
-
description = describe_music_style(music_style)
|
| 73 |
-
tracks = free_text_search(description, 20)
|
| 74 |
-
|
| 75 |
-
# logger = get_logger()
|
| 76 |
-
# logger.warning(f"""
|
| 77 |
-
# music_style = {music_style}
|
| 78 |
-
# music_style_description = {description}
|
| 79 |
-
# tracks = {pprint.pformat(tracks)}""")
|
| 80 |
-
|
| 81 |
-
# we store the tracks in a global variable so that we can access them later
|
| 82 |
-
Globals.music_styles_to_tracks[description] = tracks
|
| 83 |
-
# we return only the description to the user
|
| 84 |
-
return description
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
@timing
|
| 88 |
-
def describe_music_style(music_style: str) -> str:
|
| 89 |
-
"A tool used to describe music styles"
|
| 90 |
-
llm_describe = ChatOpenAI(temperature=0.0, model="gpt-3.5-turbo")
|
| 91 |
-
prompt_describe = ChatPromptTemplate.from_messages(
|
| 92 |
-
[
|
| 93 |
-
("system", describe_music_style_message),
|
| 94 |
-
("human", "{music_style}"),
|
| 95 |
-
]
|
| 96 |
-
)
|
| 97 |
-
runnable = prompt_describe | llm_describe | StrOutputParser()
|
| 98 |
-
return runnable.invoke(
|
| 99 |
-
{"music_style": music_style}, RunnableConfig(verbose=True, recursion_limit=1)
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
|
| 103 |
@timing
|
| 104 |
def get_llm_with_tools():
|
| 105 |
# We instantiate the Chat Model and bind the tool to it.
|
|
@@ -124,7 +85,7 @@ def get_llm_with_tools():
|
|
| 124 |
|
| 125 |
|
| 126 |
@timing
|
| 127 |
-
def get_agent_executor_from_user_id(user_id) -> AgentExecutor:
|
| 128 |
"Returns an agent executor for a given user_id"
|
| 129 |
memory = conversation_memories[user_id]
|
| 130 |
|
|
@@ -143,7 +104,6 @@ def get_agent_executor_from_user_id(user_id) -> AgentExecutor:
|
|
| 143 |
|
| 144 |
return AgentExecutor(
|
| 145 |
agent=agent,
|
| 146 |
-
# tools=[get_music_style_description],
|
| 147 |
tools=[],
|
| 148 |
memory=memory,
|
| 149 |
callbacks=Globals.handlers,
|
|
@@ -155,22 +115,7 @@ def get_agent_executor_from_user_id(user_id) -> AgentExecutor:
|
|
| 155 |
|
| 156 |
|
| 157 |
@timing
|
| 158 |
-
def
|
| 159 |
-
"Given a list of intermediate steps, returns the tracks from the last get_music_style_description action"
|
| 160 |
-
if len(intermediate_steps) == 0:
|
| 161 |
-
return []
|
| 162 |
-
|
| 163 |
-
for action_message, prompt in intermediate_steps[::-1]:
|
| 164 |
-
if action_message.tool == "get_music_style_description":
|
| 165 |
-
tracks = Globals.music_styles_to_tracks.get(prompt, [])
|
| 166 |
-
return tracks
|
| 167 |
-
|
| 168 |
-
# if none of the actions is get_music_style_description, return empty list
|
| 169 |
-
return []
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
@timing
|
| 173 |
-
def llm_inference(message: str, history: Any, user_id: str) -> Dict[str, Any]:
|
| 174 |
"""
|
| 175 |
This function is called by the API and returns the conversation response
|
| 176 |
along with the appropriate tracks and costs of the conversation so far
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from collections import defaultdict
|
| 3 |
from operator import itemgetter
|
| 4 |
from typing import Any, Dict, List
|
| 5 |
|
| 6 |
+
from langchain.agents import AgentExecutor
|
| 7 |
from langchain.agents.format_scratchpad import format_to_openai_functions
|
| 8 |
from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
|
| 9 |
from langchain.callbacks import get_openai_callback
|
| 10 |
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
|
| 11 |
from langchain.chat_models import ChatOpenAI
|
| 12 |
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 13 |
+
from langchain.schema.runnable import RunnableLambda, RunnablePassthrough
|
| 14 |
+
from langfuse.callback import BaseCallbackHandler, CallbackHandler
|
|
|
|
|
|
|
| 15 |
|
| 16 |
from app.cyanite import free_text_search
|
| 17 |
from app.logger import get_logger
|
| 18 |
from app.utils import timing
|
| 19 |
|
| 20 |
+
# system_message = """You are an agent which recommends songs based on music styles provided by the user.
|
| 21 |
+
# - A music style could be a combination of instruments, genres or sounds.
|
| 22 |
+
# - Use get_music_style_description to generate a description of the user's music style.
|
| 23 |
+
# - The styles might contain pop-culture references (artists, movies, TV-Shows, etc) You should include them when generating descriptions.
|
| 24 |
+
# - Comment on the description of the style and wish the user to enjoy the recommended songs (he will have received them).
|
| 25 |
+
# - Do not mention any songs or artists, nor give a list of songs.
|
| 26 |
+
# Write short responses with a respectful and friendly tone.
|
| 27 |
+
# """
|
| 28 |
|
| 29 |
|
| 30 |
describe_music_style_message = """You receive a music style and your goal is to describe it further with genres, instruments and sounds.
|
|
|
|
| 39 |
|
| 40 |
class Globals:
|
| 41 |
llm_with_tools: Any = None
|
| 42 |
+
handlers: List[BaseCallbackHandler] = []
|
|
|
|
| 43 |
conversation_costs: Dict[str, float] = defaultdict(float)
|
| 44 |
|
| 45 |
|
|
|
|
| 52 |
]
|
| 53 |
)
|
| 54 |
|
| 55 |
+
conversation_memories: Dict[str, ConversationBufferWindowMemory] = defaultdict(
|
| 56 |
lambda: ConversationBufferWindowMemory(
|
| 57 |
memory_key=MEMORY_KEY, return_messages=True, output_key="output", k=4
|
| 58 |
)
|
|
|
|
| 61 |
# global dicts to store the tracks and the conversation costs
|
| 62 |
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
@timing
|
| 65 |
def get_llm_with_tools():
|
| 66 |
# We instantiate the Chat Model and bind the tool to it.
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
@timing
|
| 88 |
+
def get_agent_executor_from_user_id(user_id: str) -> AgentExecutor:
|
| 89 |
"Returns an agent executor for a given user_id"
|
| 90 |
memory = conversation_memories[user_id]
|
| 91 |
|
|
|
|
| 104 |
|
| 105 |
return AgentExecutor(
|
| 106 |
agent=agent,
|
|
|
|
| 107 |
tools=[],
|
| 108 |
memory=memory,
|
| 109 |
callbacks=Globals.handlers,
|
|
|
|
| 115 |
|
| 116 |
|
| 117 |
@timing
|
| 118 |
+
def llm_inference(message: str, user_id: str) -> Dict[str, Any]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
"""
|
| 120 |
This function is called by the API and returns the conversation response
|
| 121 |
along with the appropriate tracks and costs of the conversation so far
|