juanluisrto commited on
Commit
034eb7d
·
1 Parent(s): ab039bc

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +121 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, json, random, logging
2
+ from typing import List
3
+ from dotenv import load_dotenv
4
+
5
+ from langchain.agents import AgentType, initialize_agent
6
+ from langchain.chat_models import ChatOpenAI
7
+ from langchain.tools import Tool
8
+
9
+ from langchain.schema import SystemMessage
10
+ from langchain.agents import OpenAIFunctionsAgent
11
+ from langchain.prompts import MessagesPlaceholder
12
+ from langchain.agents import AgentExecutor, OpenAIFunctionsAgent
13
+ from langchain.chains.conversation.memory import ConversationBufferMemory
14
+ from langchain.chat_models import ChatOpenAI
15
+ from langchain.agents import tool, AgentExecutor, OpenAIFunctionsAgent, AgentType, Agent
16
+ from langchain.schema import SystemMessage
17
+ from langchain.prompts import MessagesPlaceholder
18
+ from langchain.chains.conversation.memory import ConversationBufferMemory
19
+ from langchain.chat_models import ChatOpenAI
20
+
21
+ from langchain.prompts import ChatPromptTemplate
22
+ from langchain.schema import StrOutputParser
23
+
24
+ import gradio as gr
25
+
26
+ load_dotenv()
27
+
28
+
29
+
30
+ llm = ChatOpenAI(temperature=0)
31
+
32
+
33
+
34
+ from typing import List, Dict
35
+
36
+ @tool
37
+ def describe_popculture_references(references: List) -> Dict:
38
+ "A tool used to describe pop-culture references as music styles"
39
+ prompt = ChatPromptTemplate.from_messages([
40
+ ("system", """You receive a list of pop-culture references (like TV-Shows, films, artists, famous people, etc).
41
+ For each reference, write a few words separated by commas which captures the essence of it. Use music styles, sounds and instruments.
42
+ Return a dict with the references as keys and music styles as values.
43
+ """),
44
+ ("human", "{references_list}"),
45
+ ])
46
+ runnable = prompt | llm | StrOutputParser()
47
+ return runnable.invoke({"references_list" : references})
48
+
49
+
50
+ @tool
51
+ def extract_popculture_references(input_style: str) -> List:
52
+ "A tool used to extract pop-culture references from a piece of text"
53
+ prompt = ChatPromptTemplate.from_messages([
54
+ ("system", """You detect elements of the pop-culture (like TV-Shows, films, artists, famous people, etc) in the human's input message.
55
+ Return a list with these elements only. If there are none, return an empty list.
56
+ """),
57
+ ("human", "{input_style}"),
58
+ ])
59
+ runnable = prompt | llm | StrOutputParser()
60
+ output = runnable.invoke({"input_style" : input_style})
61
+ return output
62
+
63
+ @tool
64
+ def call_music_recommendation_api(input : str) -> List[str]:
65
+ """
66
+ Calls the music recommendation API
67
+ """
68
+ print("Calling music recommendation API: ", input)
69
+ return {"songs" : [input]}
70
+
71
+ tools = [describe_popculture_references, extract_popculture_references, call_music_recommendation_api]
72
+
73
+
74
+
75
+ system_message = SystemMessage(content =
76
+ """You are an agent which recommends songs based on the style a user gives.
77
+ You follow the following conversation protocol:
78
+ - You start the conversation by asking the user what style of music they like
79
+ - The user responds with a style of music
80
+ - If there are pop culture references like a movie, a TV show, an artist, a famous person, extract them AND then describe them as music styles.
81
+ - Ask the user if he is ok with the new generated style
82
+ - If the user agrees, call the music recommendation API with this style.
83
+ """)
84
+
85
+
86
+ MEMORY_KEY = "chat_history"
87
+ prompt = OpenAIFunctionsAgent.create_prompt(
88
+ system_message=system_message,
89
+ extra_prompt_messages=[MessagesPlaceholder(variable_name=MEMORY_KEY)]
90
+ )
91
+
92
+ memory = ConversationBufferMemory(memory_key=MEMORY_KEY, return_messages=True)
93
+
94
+
95
+ agent = OpenAIFunctionsAgent(
96
+ llm=llm,
97
+ tools=tools,
98
+ prompt=prompt,
99
+ agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION
100
+ )
101
+
102
+ agent_executor = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True)
103
+
104
+
105
+ def inference(message, history):
106
+ # return agent_executor.run(message)
107
+ for chunk in agent_executor.stream(message):
108
+ yield chunk["output"]
109
+
110
+
111
+ gr.ChatInterface(
112
+ inference,
113
+ chatbot=gr.Chatbot(height=400),
114
+ textbox=gr.Textbox(placeholder="Ask me for music recommendations!", container=False, scale=7),
115
+ description="This AI makes song recommendations based on your music style.",
116
+ title="Persona Music song recommender",
117
+ examples=["Recommend me something in Quentin Tarantino reggae style", "Give me songs with calm and relaxing vibes", "I want to listen to something like the movie Inception", "I want music that sounds like Lebron James eating soup"],
118
+ retry_btn="Retry",
119
+ clear_btn="Clear",
120
+ undo_btn = None,
121
+ ).queue().launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ langchain
2
+ openai
3
+ python-dotenv