Denyol commited on
Commit
a3cc1c6
·
verified ·
1 Parent(s): 5c34cb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -20
app.py CHANGED
@@ -1,9 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import os
3
  import cohere
4
 
5
 
6
- COHERE_API_KEY = os.getenv("COHERE_API_KEY")
7
  client = cohere.ClientV2(COHERE_API_KEY)
8
 
9
  COHERE_MODEL = "command-r-plus"
@@ -16,22 +55,29 @@ def respond(
16
  top_p,
17
  ):
18
 
19
- system_message = '''
20
- You are a friendly video game recommendation expert chatbot.
 
 
 
21
  Your task is to help parents and guardians to find appropriate video games for their children.
22
  Extract the child's age, preferred genre and multiplayer preference.
23
 
24
- After you extracted the information you need, you should:
25
- - Suggest 5 video games that fit the given criteria.
26
- - If no games exactly match the genre, suggest similar alternatives.
27
  - If multiplayer is required, only include games with co-op mode.
28
 
 
 
 
 
29
  ### Response Format:
30
- Game 1:
31
- - Name: [Game Title]
32
- - Genre: [Genre]
33
- - Age Suitability: [Age Group]
34
- - Multiplayer: [Yes/No]
35
  - Description: [Short summary of the game]
36
  - The reasons why you recommend the game
37
 
@@ -45,19 +91,18 @@ def respond(
45
  If they are not satisfied, then give the user the options of receiving more recommendations or changing their preferences.
46
  If they have questions about a game/games then provide the user with real information about the game/games.
47
  If they are satisifed and have no questions, then tell them that you were very happy to help and end the conversation.
48
- '''
49
 
50
- messages = [{"role": "system", "content": system_message}]
51
-
52
- messages.append({"role": "user", "content": message})
53
 
54
  response = ""
55
-
56
  response = client.chat(
57
- messages=messages,
58
- model=COHERE_MODEL,
59
- temperature=temperature,
60
- max_tokens=max_tokens,
61
  )
62
  yield response.message.content[0].text
63
 
 
1
+ from langchain.prompts import ChatPromptTemplate
2
+ from langchain_community.document_loaders import JSONLoader
3
+ from langchain_huggingface import HuggingFaceEmbeddings
4
+ from langchain_community.vectorstores import Chroma
5
+ from langchain_cohere import ChatCohere
6
+ from langchain_core.output_parsers import StrOutputParser
7
+ from langchain_core.runnables import RunnableLambda, RunnablePassthrough
8
+
9
+
10
+ embedding_function = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
11
+
12
+ loader = JSONLoader(file_path="games.json", jq_schema=".games[]", text_content=False)
13
+ documents = loader.load()
14
+
15
+ db = Chroma.from_documents(documents, embedding_function)
16
+
17
+ retriever = db.as_retriever(
18
+ search_type="mmr",
19
+ search_kwargs={'k': 500, 'fetch_k': 500}
20
+ )
21
+
22
+ template = """Answer the question based only on the following context:
23
+ {context}
24
+
25
+ Question: {question}
26
+ """
27
+ prompt = ChatPromptTemplate.from_template(template)
28
+
29
+ COHERE_API_KEY = os.getenv("COHERE_API_KEY")
30
+
31
+ model = ChatCohere()
32
+
33
+ chain = (
34
+ {"context": retriever, "question": RunnablePassthrough()}
35
+ | prompt
36
+ | model
37
+ | StrOutputParser()
38
+ )
39
+
40
+
41
  import gradio as gr
42
  import os
43
  import cohere
44
 
45
 
 
46
  client = cohere.ClientV2(COHERE_API_KEY)
47
 
48
  COHERE_MODEL = "command-r-plus"
 
55
  top_p,
56
  ):
57
 
58
+ query = message
59
+ retrieved_response = chain.invoke(query)
60
+
61
+ system_message = f"""
62
+ You are a friendly video game recommendation expert chatbot.
63
  Your task is to help parents and guardians to find appropriate video games for their children.
64
  Extract the child's age, preferred genre and multiplayer preference.
65
 
66
+ After you extracted the information you need, you should:
67
+ - Suggest 5 video games that fit the given criteria.
68
+ - If no games exactly match the genre, suggest similar alternatives.
69
  - If multiplayer is required, only include games with co-op mode.
70
 
71
+ Use the following game info to generate your suggestions:
72
+ {retrieved_response}
73
+ If you don't find enough games in the info you are given, then use your own knowledge.
74
+
75
  ### Response Format:
76
+ Game 1:
77
+ - Name: [Game Title]
78
+ - Genre: [Genre]
79
+ - Age Suitability: [Age Group]
80
+ - Multiplayer: [Yes/No]
81
  - Description: [Short summary of the game]
82
  - The reasons why you recommend the game
83
 
 
91
  If they are not satisfied, then give the user the options of receiving more recommendations or changing their preferences.
92
  If they have questions about a game/games then provide the user with real information about the game/games.
93
  If they are satisifed and have no questions, then tell them that you were very happy to help and end the conversation.
94
+ """
95
 
96
+ messages = [{"role": "system", "content": system_message},
97
+ {"role": "user", "content": message}]
 
98
 
99
  response = ""
100
+
101
  response = client.chat(
102
+ messages=messages,
103
+ model=COHERE_MODEL,
104
+ temperature=temperature,
105
+ max_tokens=max_tokens,
106
  )
107
  yield response.message.content[0].text
108