mirror of
https://github.com/OS-Copilot/OS-Copilot.git
synced 2026-05-05 03:00:15 -04:00
图片搜索api
This commit is contained in:
@@ -3,7 +3,7 @@ from langchain.utilities import BingSearchAPIWrapper
|
||||
from bs4 import BeautifulSoup
|
||||
from typing import Tuple
|
||||
from enum import Enum
|
||||
from web_loader import WebPageLoader
|
||||
from .web_loader import WebPageLoader
|
||||
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
||||
from langchain.embeddings.openai import OpenAIEmbeddings
|
||||
from langchain.vectorstores import Chroma
|
||||
|
||||
@@ -3,6 +3,7 @@ from pydantic import BaseModel,Field
|
||||
from typing import Optional
|
||||
from .bing_api import BingAPI
|
||||
from .bing_api_v2 import BingAPIV2
|
||||
from .image_search_api import ImageSearchAPI
|
||||
import tiktoken
|
||||
|
||||
# 计算网页内容对gpt4来说的token数,如果token太多就用3.5做摘要或者用向量数据库检索最相关的片段
|
||||
@@ -16,6 +17,7 @@ router = APIRouter()
|
||||
|
||||
bing_api = BingAPI('885e62a126554fb390af88ae31d2c8ff')
|
||||
bing_api_v2 = BingAPIV2()
|
||||
image_search_api = ImageSearchAPI('885e62a126554fb390af88ae31d2c8ff')
|
||||
|
||||
# class QueryItem(BaseModel):
|
||||
# query: str
|
||||
@@ -48,6 +50,16 @@ class PageItemV2(BaseModel):
|
||||
# raise HTTPException(status_code=500, detail=page_detail)
|
||||
# return {"page_content": page_detail}
|
||||
|
||||
@router.get("/tools/bing/image_search")
|
||||
async def image_search(item: QueryItemV2):
|
||||
try:
|
||||
if item.top_k == None:
|
||||
item.top_k = 10
|
||||
search_results = image_search_api.search_image(item.query,item.top_k)
|
||||
except RuntimeError as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
return search_results
|
||||
|
||||
@router.get("/tools/bing/searchv2")
|
||||
async def bing_search_v2(item: QueryItemV2):
|
||||
try:
|
||||
|
||||
45
jarvis/api/bing/image_search_api.py
Normal file
45
jarvis/api/bing/image_search_api.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from typing import Tuple
|
||||
from enum import Enum
|
||||
|
||||
SEARCH_RESULT_LIST_CHUNK_SIZE = 3
|
||||
RESULT_TARGET_PAGE_PER_TEXT_COUNT = 500
|
||||
|
||||
|
||||
class ImageSearchAPI:
|
||||
def __init__(self, subscription_key: str) -> None:
|
||||
self._headers = {
|
||||
'Ocp-Apim-Subscription-Key': subscription_key,
|
||||
'BingAPIs-Market': 'en-US',
|
||||
|
||||
}
|
||||
self._endpoint = "https://api.bing.microsoft.com/v7.0/images/search"
|
||||
self._mkt = 'en-US'
|
||||
|
||||
def search_image(self, key_words: str,top_k: int=10, max_retry: int = 3):
|
||||
|
||||
for _ in range(max_retry):
|
||||
try:
|
||||
result = requests.get(self._endpoint, headers=self._headers, params={'q': key_words, 'mkt': self._mkt,'safeSearch' : 'moderate'},
|
||||
timeout=10)
|
||||
except Exception:
|
||||
continue
|
||||
if result.status_code == 200:
|
||||
result = result.json()
|
||||
image_List = []
|
||||
if result != None:
|
||||
image_List = [
|
||||
{
|
||||
"name": item["name"],
|
||||
"imageUrl": item["thumbnailUrl"],
|
||||
"imageSize": item["thumbnail"]
|
||||
} for item in result["value"]
|
||||
]
|
||||
if(len(image_List) > top_k):
|
||||
image_List = image_List[:top_k]
|
||||
return image_List
|
||||
else:
|
||||
continue
|
||||
raise RuntimeError("Failed to access Bing Search API.")
|
||||
|
||||
@@ -5,42 +5,6 @@
|
||||
"version": "0.1.0"
|
||||
},
|
||||
"paths": {
|
||||
"/tools/python": {
|
||||
"post": {
|
||||
"summary": "python interpreter, can run python code",
|
||||
"operationId": "execute_python_tools_python_post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Item"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/tools/calculator": {
|
||||
"post": {
|
||||
"summary": "math calculator,can be used to perform simple quadratic operations ",
|
||||
@@ -77,6 +41,42 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/tools/bing/image_search": {
|
||||
"get": {
|
||||
"summary": "Image Search API can return top k related image data based on keywords, including image name, download address and size.",
|
||||
"operationId": "image_search_tools_bing_image_search_get",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/QueryItemV2"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/tools/arxiv": {
|
||||
"get": {
|
||||
"summary": "tool to get Arxiv Article Information",
|
||||
@@ -186,240 +186,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/tools/shell": {
|
||||
"post": {
|
||||
"summary": "Can be used to run some bash commands",
|
||||
"operationId": "execute_shell_command_tools_shell_post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ShellCommandModel"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/ShellCommandResultModel"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/tools/ppt/create_file": {
|
||||
"post": {
|
||||
"summary": "can be used to create ppt",
|
||||
"operationId": "create_file_tools_ppt_create_file_post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/CreateFileModel"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/tools/ppt/get_image": {
|
||||
"post": {
|
||||
"summary": "can be used to get an image from ppt",
|
||||
"operationId": "get_image_tools_ppt_get_image_post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/GetImageModel"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/tools/ppt/add_first_page": {
|
||||
"post": {
|
||||
"summary": "Add First Page to the ppt",
|
||||
"operationId": "add_first_page_tools_ppt_add_first_page_post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/AddFirstPageModel"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/tools/ppt/add_text_page": {
|
||||
"post": {
|
||||
"summary": "Add Text Page to the ppt",
|
||||
"operationId": "add_text_page_tools_ppt_add_text_page_post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/AddTextPageModel"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/tools/ppt/add_text_image_page": {
|
||||
"post": {
|
||||
"summary": "Add Text Image Page to the ppt",
|
||||
"operationId": "add_text_image_page_tools_ppt_add_text_image_page_post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/AddTextImagePageModel"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/tools/ppt/submit_file": {
|
||||
"get": {
|
||||
"summary": "Submit ppt File",
|
||||
"operationId": "submit_file_tools_ppt_submit_file_get",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/tools/database": {
|
||||
"post": {
|
||||
"summary": "Execute Sqlite script for the travel datebase in the server",
|
||||
@@ -538,42 +304,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/calendar/insert_event": {
|
||||
"post": {
|
||||
"summary": "Insert Event to the Calendar",
|
||||
"operationId": "insert_event_calendar_insert_event_post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/CalendarEvent"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/gmail/send": {
|
||||
"post": {
|
||||
"summary": "Send google Email",
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user