Thomas Richardson commited on
Commit
fb594a7
·
unverified ·
2 Parent(s): 3bc82dddd2da3e

Merge pull request #49 from ttt246/feature/browser_select_item_39

Browse files
app.py CHANGED
@@ -2,12 +2,17 @@ from src.firebase.firebase import initialize_app
2
  from fastapi import Depends, FastAPI
3
  import uvicorn
4
 
 
 
5
  initialize_app()
6
 
7
  from src.router.api import construct_blueprint_api
8
 
9
  app = FastAPI()
10
- app.include_router(construct_blueprint_api(), tags=["ai"])
 
 
 
11
 
12
 
13
  if __name__ == "__main__":
 
2
  from fastapi import Depends, FastAPI
3
  import uvicorn
4
 
5
+ from src.router.browser_router import construct_blueprint_browser_api
6
+
7
  initialize_app()
8
 
9
  from src.router.api import construct_blueprint_api
10
 
11
  app = FastAPI()
12
+ app.include_router(construct_blueprint_api(), tags=["ai_app"])
13
+ app.include_router(
14
+ construct_blueprint_browser_api(), prefix="/browser", tags=["ai_browser"]
15
+ )
16
 
17
 
18
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -67,4 +67,5 @@ win32-setctime==1.1.0
67
  wrapt==1.15.0
68
  yarl==1.8.2
69
  twilio==8.2.1
70
- nemoguardrails==0.2.0
 
 
67
  wrapt==1.15.0
68
  yarl==1.8.2
69
  twilio==8.2.1
70
+ nemoguardrails==0.2.0
71
+ user-agents==2.2.0
src/model/requests/request_model.py CHANGED
@@ -1,4 +1,51 @@
 
 
1
  from pydantic import BaseModel
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
 
4
  class BasicReq(BaseModel):
@@ -78,3 +125,15 @@ class TrainContacts(BasicReq):
78
  status: str
79
 
80
  contacts: list[ContactReq]
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+
3
  from pydantic import BaseModel
4
+ from fastapi import Depends, Request, HTTPException
5
+ from user_agents import parse
6
+
7
+
8
+ class ClientInfo:
9
+ def __init__(self, browser, os, device_type):
10
+ self.browser = browser
11
+ self.os = os
12
+ self.device_type = device_type
13
+
14
+ def is_browser(self) -> bool:
15
+ if (
16
+ self.browser == "Chrome"
17
+ or self.browser == "Firefox"
18
+ or self.browser == "Safari"
19
+ or self.browser == "Edge"
20
+ ):
21
+ return True
22
+ return False
23
+
24
+
25
+ def parse_user_agent(user_agent: str) -> Optional[ClientInfo]:
26
+ if not user_agent:
27
+ return None
28
+
29
+ ua = parse(user_agent)
30
+
31
+ device_type = "desktop" if ua.is_pc else "mobile" if ua.is_mobile else "tablet"
32
+ client_info = ClientInfo(
33
+ browser=ua.browser.family, os=ua.os.family, device_type=device_type
34
+ )
35
+
36
+ return client_info
37
+
38
+
39
+ def get_client_info(request: Request):
40
+ user_agent = request.headers.get("user-agent", "")
41
+
42
+ if not user_agent:
43
+ raise HTTPException(
44
+ status_code=400,
45
+ detail="User-Agent header is required",
46
+ )
47
+
48
+ return parse_user_agent(user_agent)
49
 
50
 
51
  class BasicReq(BaseModel):
 
125
  status: str
126
 
127
  contacts: list[ContactReq]
128
+
129
+
130
+ """endpoint /browser/item"""
131
+
132
+
133
+ class BrowserItem(BasicReq):
134
+ class ItemReq(BaseModel):
135
+ title: str
136
+ link: str
137
+
138
+ items: list[ItemReq]
139
+ prompt: str
src/rising_plugin/guardrails-config/actions/phone.csv CHANGED
@@ -6,14 +6,15 @@
6
  "If user said that send sms or text, please answer belowing json format. {""program"": ""sms"", ""content"": ""ask who""}"
7
  "If user said that compose, write, or create an sms message, please answer belowing json format. {""program"": ""sms"", ""content"": ""ask who""}"
8
  "If user said that search contact with its description such as display name or phone number, please answer belowing json format. {""program"": ""contact"", ""content"": ""description of the contact that user is going to search""}"
9
- "If user said that open a new tab, go to a new tab, or open a new page, please answer belowing json format. {""program"": ""open_tab"", ""content"": """"}"
10
- "If user said that open a new tab and search, go to a new tab and search, or open a new page and search, please answer belowing json format. {""program"": ""open_tab_search"", ""content"": ""keyword that user is going to search""}"
11
- "If user said that close a tab, please answer belowing json format. {""program"": ""close_tab"", ""content"": """"}"
12
- "If user said that launch a browser or open a browser, please answer belowing json format. {""program"": ""browser"", ""content"": ""https://google.com""}"
13
- "If user said that go to a previous page, or open a previous page, please answer belowing json format. {""program"": ""previous_page"", ""content"": """"}"
14
- "If user said that go to a next page, or open a next page, please answer belowing json format. {""program"": ""next_page"", ""content"": """"}"
15
- "If user said that scroll up, scroll up page, or page scroll up, please answer belowing json format. {""program"": ""scroll_up"", ""content"": """"}"
16
- "If user said that scroll down, scroll down page, page scroll down, please answer belowing json format. {""program"": ""scroll_down"", ""content"": """"}"
17
- "If user said that scroll top, scroll top page, or scroll top of page, please answer belowing json format. {""program"": ""scroll_top"", ""content"": """"}"
18
- "If user said that scroll bottom, scroll bottom page, or scroll bottom of page, please answer belowing json format. {""program"": ""scroll_bottom"", ""content"": """"}"
 
19
  "If all of above is not correct, please give the most appropriate answer to the user's question. Please answer belowing json format. {""program"":""message"", ""content"":""your answer""}"
 
6
  "If user said that send sms or text, please answer belowing json format. {""program"": ""sms"", ""content"": ""ask who""}"
7
  "If user said that compose, write, or create an sms message, please answer belowing json format. {""program"": ""sms"", ""content"": ""ask who""}"
8
  "If user said that search contact with its description such as display name or phone number, please answer belowing json format. {""program"": ""contact"", ""content"": ""description of the contact that user is going to search""}"
9
+ "If user said that open a new tab, go to a new tab, or open a new page in a web browser, please answer belowing json format. {""program"": ""open_tab"", ""content"": """"}"
10
+ "If user said that open a new tab and search, go to a new tab and search in a web browser, or open a new page and search, please answer belowing json format. {""program"": ""open_tab_search"", ""content"": ""keyword that user is going to search""}"
11
+ "If user said that close a tab, please answer belowing json format in a web browser. {""program"": ""close_tab"", ""content"": """"}"
12
+ "If user said that launch a browser or open a browser in a web browser, please answer belowing json format. {""program"": ""browser"", ""content"": ""https://google.com""}"
13
+ "If user said that go to a previous page, or open a previous page in a web browser, please answer belowing json format. {""program"": ""previous_page"", ""content"": """"}"
14
+ "If user said that go to a next page, or open a next page in a web browser, please answer belowing json format. {""program"": ""next_page"", ""content"": """"}"
15
+ "If user said that scroll up, scroll up page, or page scroll up in a web browser, please answer belowing json format. {""program"": ""scroll_up"", ""content"": """"}"
16
+ "If user said that scroll down, scroll down page, page scroll down in a web browser, please answer belowing json format. {""program"": ""scroll_down"", ""content"": """"}"
17
+ "If user said that scroll top, scroll top page, or scroll top of page in a web browser, please answer belowing json format. {""program"": ""scroll_top"", ""content"": """"}"
18
+ "If user said that scroll bottom, scroll bottom page, or scroll bottom of page in a web browser, please answer belowing json format. {""program"": ""scroll_bottom"", ""content"": """"}"
19
+ "If user is going to select an item, an article or a website with its description in a web browser, please answer belowing json format. {""program"": ""select_item_detail_info"", ""content"": ""the description of an item, an article or a website in a browser""}"
20
  "If all of above is not correct, please give the most appropriate answer to the user's question. Please answer belowing json format. {""program"":""message"", ""content"":""your answer""}"
src/rising_plugin/guardrails-config/actions/phone.json CHANGED
The diff for this file is too large to render. See raw diff
 
src/rising_plugin/risingplugin.py CHANGED
@@ -7,6 +7,7 @@ import textwrap
7
 
8
  from typing import Any
9
 
 
10
  from nemoguardrails.rails import LLMRails, RailsConfig
11
 
12
  from langchain.chat_models import ChatOpenAI
@@ -152,6 +153,17 @@ def getCompletion(
152
  return processLargeText(app, chunks)
153
 
154
 
 
 
 
 
 
 
 
 
 
 
 
155
  def query_image_ask(image_content, message, uuid):
156
  prompt_template = get_prompt_image_with_message(image_content, message)
157
  try:
 
7
 
8
  from typing import Any
9
 
10
+ from langchain.chains.question_answering import load_qa_chain
11
  from nemoguardrails.rails import LLMRails, RailsConfig
12
 
13
  from langchain.chat_models import ChatOpenAI
 
153
  return processLargeText(app, chunks)
154
 
155
 
156
+ def getCompletionOnly(
157
+ query: str,
158
+ model: str = "gpt-4",
159
+ ) -> str:
160
+ llm = ChatOpenAI(model_name=model, temperature=1.7, openai_api_key=OPENAI_API_KEY)
161
+ chain = load_qa_chain(llm, chain_type="stuff")
162
+ test_question = """Please return the link of best relatedness of item which the title is "Android Studio in browser" from the below data. [{"title": "Android Studio", "link": "https://android.com"} , {"title": "What's this?", "link": "https://test.com"} , {"title": "How are you?", "link": "https://d.com"}]"""
163
+ chain_data = chain.run(input_documents=[], question=test_question)
164
+ return chain_data
165
+
166
+
167
  def query_image_ask(image_content, message, uuid):
168
  prompt_template = get_prompt_image_with_message(image_content, message)
169
  try:
src/router/api.py CHANGED
@@ -13,6 +13,8 @@ from src.model.requests.request_model import (
13
  SendSMS,
14
  TrainContacts,
15
  BasicReq,
 
 
16
  )
17
  from src.rising_plugin.risingplugin import (
18
  getCompletion,
@@ -33,7 +35,7 @@ from src.service.feedback_service import FeedbackService
33
  from src.service.llm.chat_service import ChatService
34
  from src.service.twilio_service import TwilioService
35
 
36
- from fastapi import APIRouter
37
 
38
  router = APIRouter()
39
 
@@ -55,10 +57,15 @@ def construct_blueprint_api() -> APIRouter:
55
  )"""
56
 
57
  @router.post("/sendNotification")
58
- def send_notification(data: Notification):
 
 
59
  query = data.message
60
  token = data.token
61
  uuid = data.uuid
 
 
 
62
 
63
  result = getCompletion(query=query, uuid=uuid)
64
 
 
13
  SendSMS,
14
  TrainContacts,
15
  BasicReq,
16
+ ClientInfo,
17
+ get_client_info,
18
  )
19
  from src.rising_plugin.risingplugin import (
20
  getCompletion,
 
35
  from src.service.llm.chat_service import ChatService
36
  from src.service.twilio_service import TwilioService
37
 
38
+ from fastapi import APIRouter, Request, Depends
39
 
40
  router = APIRouter()
41
 
 
57
  )"""
58
 
59
  @router.post("/sendNotification")
60
+ def send_notification(
61
+ data: Notification, client_info: ClientInfo = Depends(get_client_info)
62
+ ):
63
  query = data.message
64
  token = data.token
65
  uuid = data.uuid
66
+ # check browser endpoint
67
+ if client_info.is_browser():
68
+ query = f"{query} in a web browser"
69
 
70
  result = getCompletion(query=query, uuid=uuid)
71
 
src/router/browser_router.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Request, Depends
2
+
3
+ from src.common.assembler import Assembler
4
+ from src.model.requests.request_model import BrowserItem
5
+ from src.service.browser_service import BrowserService
6
+
7
+ router = APIRouter()
8
+
9
+
10
+ def construct_blueprint_browser_api() -> APIRouter:
11
+ # Assembler
12
+ assembler = Assembler()
13
+ # Services
14
+ browser_service = BrowserService()
15
+ """@generator.request_body(
16
+ {
17
+ "token": "String",
18
+ "uuid": "String",
19
+ "items":["title": "String", "link": "String"],
20
+ "prompt":"String",
21
+ }
22
+ )
23
+ @generator.response(
24
+ status_code=200, schema={"message": "message", "result": "test_result"}
25
+ )"""
26
+
27
+ @router.post("/item")
28
+ def get_item(data: BrowserItem):
29
+ item_link = ""
30
+ try:
31
+ token = data.token
32
+ uuid = data.uuid
33
+
34
+ # parsing contacts
35
+ # train contact
36
+ item_link = browser_service.query_item(items=data.items, query=data.prompt)
37
+ except Exception as e:
38
+ return assembler.to_response(400, "Failed to get item in a browser", "")
39
+ return assembler.to_response(
40
+ 200, "Getting an item in a browser successfully", item_link
41
+ )
42
+
43
+ return router
src/service/browser_service.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """service to handle & process the browser"""
2
+ from src.model.requests.request_model import BrowserItem
3
+ from src.rising_plugin.risingplugin import getCompletionOnly
4
+
5
+
6
+ class BrowserService:
7
+ """query to get the link of the item from the list"""
8
+
9
+ def query_item(self, items: list[BrowserItem.ItemReq], query: str) -> str:
10
+ prompt_template = f"Please return the link of best relatedness of item which the title is '{query}' from the below data.\n {items}"
11
+ return getCompletionOnly(query=prompt_template)