Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
mjkweon17 committed Mar 23, 2024
1 parent 0752b70 commit 8bc2ae2
Show file tree
Hide file tree
Showing 18 changed files with 429 additions and 206 deletions.
Binary file modified requirements/prod.txt
Binary file not shown.
84 changes: 36 additions & 48 deletions src/bookmarks/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from database import get_db
from models import Bookmark, Page
from bookmarks.schemas import BookmarkCreate, BookmarkResponse
from bookmarks.service import get_bookmarks_service
from bookmarks.service import get_bookmarks_service, post_bookmarks_service, get_related_bookmarks

router = APIRouter(
prefix="/bookmarks",
Expand Down Expand Up @@ -36,45 +36,7 @@ def create_bookmark(
bookmark: BookmarkCreate,
db: Session = Depends(get_db)
):
page = db.query(Page).filter(Page.url == bookmark.url).first()
if not page:
# Page 만들기
page = Page(
title="temporay title",
url=bookmark.url,
summary="temporay summary",
created_at=datetime.now(),
state=1
)
db.add(page)
db.commit()
db.refresh(page)

page = db.query(Page).filter(Page.url == bookmark.url).first()

db_bookmark = Bookmark(
page_id=page.page_id,
user_id=bookmark.user_id,
created_at=datetime.now(),
state=1
)
db.add(db_bookmark)
db.commit()
db.refresh(db_bookmark)

db_bookmark = db.query(Bookmark).filter(Bookmark.bookmark_id == db_bookmark.bookmark_id).first()

response = BookmarkResponse(
user_id=db_bookmark.user_id,
url=bookmark.url,
bookmark_id=db_bookmark.bookmark_id,
page_id=db_bookmark.page_id,
created_at=db_bookmark.created_at,
title=page.title,
summary=page.summary
)

return response
return post_bookmarks_service(db, bookmark)

@router.get(
"/newest",
Expand Down Expand Up @@ -103,7 +65,7 @@ async def get_bookmarks(


@router.get(
"/frequent",
"/develop",
response_model=List[BookmarkResponse],
status_code=status.HTTP_200_OK,
description="""
Expand All @@ -119,16 +81,16 @@ async def get_bookmarks(
}
}
)
async def get_bookmarks(
async def get_develop_bookmarks(
db: Session = Depends(get_db),
user_id: int = Query(None, description="User ID", gt=0, example=1),
offset: int = Query(0, description="Offset of the first item to return", ge=0, example=0),
limit: int = Query(10, description="Maximum number of items to return", ge=1, le=100, example=10),
):
return get_bookmarks_service(db, user_id, offset, limit)
return get_related_bookmarks("develop", db, user_id, offset, limit)

@router.get(
"/forgotten",
"/fastapi",
response_model=List[BookmarkResponse],
status_code=status.HTTP_200_OK,
description="""
Expand All @@ -137,17 +99,43 @@ async def get_bookmarks(
- limit은 최대 몇 개의 데이터를 가져올지 결정. default 값은 10임. 값을 입력하지 않으면 default 값이 적용됨.
- 테스트는 어느 정도 진행했지만, 더 많은 테스트가 필요함! db에도 더 많은 데이터를 넣어서 테스트 진행해야 함.
""",
summary="망각했을 가능성이 높은 순서로 북마크 목록 조회",
summary="빈도순으로 북마크 목록 조회",
response_description={
status.HTTP_200_OK: {
"description": "망각했을 가능성이 높은 순서로 북마크 목록 조회 성공"
"description": "빈도순으로 북마크 목록 조회 성공"
}
}
)
async def get_bookmarks(
async def get_fastapi_bookmarks(
db: Session = Depends(get_db),
user_id: int = Query(None, description="User ID", gt=0, example=1),
offset: int = Query(0, description="Offset of the first item to return", ge=0, example=0),
limit: int = Query(10, description="Maximum number of items to return", ge=1, le=100, example=10),
):
return get_related_bookmarks("fastapi", db, user_id, offset, limit)

@router.get(
"/keyword",
response_model=List[BookmarkResponse],
status_code=status.HTTP_200_OK,
description="""
- Query Parameter는 아래와 같음.
- offset은 몇 번째부터 데이터를 가져올지 결정. default 값은 0임. 값을 입력하지 않으면 default 값이 적용됨.
- limit은 최대 몇 개의 데이터를 가져올지 결정. default 값은 10임. 값을 입력하지 않으면 default 값이 적용됨.
- 테스트는 어느 정도 진행했지만, 더 많은 테스트가 필요함! db에도 더 많은 데이터를 넣어서 테스트 진행해야 함.
""",
summary="keyword와 관련된 북마크 목록 조회",
response_description={
status.HTTP_200_OK: {
"description": "keyword와 관련된 북마크 목록 조회 성공"
}
}
)
async def get_fastapi_bookmarks(
db: Session = Depends(get_db),
keyword: str = Query(None, description="Keyword", example="fastapi"),
user_id: int = Query(None, description="User ID", gt=0, example=1),
offset: int = Query(0, description="Offset of the first item to return", ge=0, example=0),
limit: int = Query(10, description="Maximum number of items to return", ge=1, le=100, example=10),
):
return get_bookmarks_service(db, user_id, offset, limit)
return get_related_bookmarks(keyword, db, user_id, offset, limit)
119 changes: 115 additions & 4 deletions src/bookmarks/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,77 @@
from sqlalchemy.sql.expression import null

from models import Page, Bookmark
# from schemas import
# from utils import
from bookmarks.schemas import BookmarkResponse, BookmarkCreate
from utils.web_crawler import get_webpage_content
from utils.ai.summarizer import generate_summary
from utils.ai.get_vector import get_embedding
from utils.ai.vector_db import upload_vector
from utils.ai.vector_db import search_vector

def post_bookmarks_service(db, bookmark: BookmarkCreate) -> dict:

try:
# 웹 페이지 정보 가져오기
title, content = get_webpage_content(bookmark.url)
except Exception as e:
# 웹 페이지를 가져오는 데 실패한 경우
raise HTTPException(status_code=404, detail="웹 사이트를 찾을 수 없습니다.") from e


# content를 AI 요약 서비스에 넣어서 요약된 내용을 반환
summary = generate_summary(content)

# page에 page 내용이 있는지 확인
page = db.query(Page).filter(Page.url == bookmark.url).first()

# page가 없으면 page를 만든다
if not page:
# Page 만들기
page = Page(
title=title,
url=bookmark.url,
summary=summary,
created_at=datetime.now(),
state=1
)
db.add(page)
db.commit()
db.refresh(page)

embedding = get_embedding(summary)
vector = embedding.data[0].embedding

# 사용자가 북마크한 페이지인지 확인
db_bookmark = db.query(Bookmark).filter(Bookmark.page_id == page.page_id, Bookmark.user_id == bookmark.user_id).first()
if db_bookmark:
raise HTTPException(status_code=400, detail="이미 북마크한 페이지입니다.")

# 북마크 추가
db_bookmark = Bookmark(
page_id=page.page_id,
user_id=bookmark.user_id,
created_at=datetime.now(),
state=1
)
db.add(db_bookmark)
db.commit()
db.refresh(db_bookmark)

# 북마크 정보 반환
response = BookmarkResponse(
user_id=db_bookmark.user_id,
url=bookmark.url,
bookmark_id=db_bookmark.bookmark_id,
page_id=db_bookmark.page_id,
created_at=db_bookmark.created_at,
title=page.title,
summary=page.summary
)
return response

def get_bookmarks_service(db, user_id: int, offset: int, limit: int) -> List[dict]:
query = db.query(Page).join(Bookmark, Page.page_id == Bookmark.page_id).filter(Bookmark.user_id == user_id)
query = query.filter(Page.state == 1).offset(offset).limit(limit)
query = query.filter(Page.state == 1)

db_pages = query.all()

Expand All @@ -29,5 +94,51 @@ def get_bookmarks_service(db, user_id: int, offset: int, limit: int) -> List[dic
"created_at": db_bookmark.created_at,
"summary": db_page.summary
})

# created_at 최근 순으로 정렬
response = sorted(response, key=lambda x: x["created_at"], reverse=True)

# offset, limit 적용
response = response[offset:offset+limit]

return response







def get_related_bookmarks(keyword: str, db: Session, user_id: int, offset: int, limit: int) -> List[dict]:

# 사용자가 작성한 글의 내용을 가져온다
content = keyword

# 글을 embedding 한다
embedding = get_embedding(content)
vector = embedding.data[0].embedding

# vector db에 넣어서 top_k result를 가져온다
results = search_vector(vector, 10)

# results에는 page_id가 들어있다
# page_id를 이용해서 page 정보와 bookmark 정보를 가져온다
recommended_pages = []
for result in results:
page = db.query(Page).filter(Page.page_id == result).first()
bookmark = db.query(Bookmark).filter(Bookmark.page_id == result).first()

recommended_pages.append({
"bookmark_id": bookmark.bookmark_id,
"page_id": page.page_id,
"created_at": bookmark.created_at,
"title": page.title,
"summary": page.summary,
"url": page.url,
"user_id": bookmark.user_id
})

# offset, limit 적용
recommended_pages = recommended_pages[offset:offset+limit]

return response
return recommended_pages
4 changes: 2 additions & 2 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class DB_Settings(BaseSettings):
db_user: str = os.getenv("DB_USER")


class GenAI_Settings(BaseSettings):
openai_api_key: str = os.getenv("OPENAI_API_KEY")
class AI_Settings(BaseSettings):
openai_key: str = os.getenv("OPENAI_API_KEY")
pinecone_api_key: str = os.getenv("PINECONE_API_KEY")


Expand Down
12 changes: 3 additions & 9 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from bookmarks.router import router as bookmarks_router
from recommendations.router import router as recommendations_router
from users.router import router as users_router

from utils.web_crawler import get_title_and_content_selenium
from test.router import router as test_router

app = FastAPI(
title="Love-SKYST",
Expand All @@ -27,6 +26,7 @@
)

# 라우터 등록
# app.include_router(test_router)
app.include_router(bookmarks_router)
app.include_router(recommendations_router)

Expand All @@ -52,10 +52,4 @@
@app.get("/")
async def root():
result = {"파란만장 화이팅!"}
return result

@app.get("/content_and_title")
async def get_content_and_title(url: str):

title, content = get_title_and_content_selenium(url)
return {"title": title, "content": content}
return result
12 changes: 3 additions & 9 deletions src/recommendations/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.orm import Session

from database import get_db
from recommendations.schemas import RecommendationRequest, RecommendationResponse, BookmarkBase
from recommendations.schemas import RecommendationRequest, BookmarkBase
from recommendations.service import get_recommendations_service

router = APIRouter(
Expand All @@ -17,7 +17,7 @@

@router.post(
"/",
response_model=RecommendationResponse,
response_model=List[BookmarkBase],
status_code=status.HTTP_200_OK,
description="글 관련 페이지 추천",
summary="글 관련 페이지 추천",
Expand All @@ -31,11 +31,5 @@ async def recommend_pages(
request: RecommendationRequest,
db: Session = Depends(get_db)
):
# 사용자 정보를 가지고 구글 드라이브에 접속한다
# content_id를 가지고 온다
# 수정 사항을 embedding한다
# vector db에서 가장 가까운 n개의 문서를 가지고 온다
# n 개의 문서 중 유사도가 일정 수치 이하인 문서는 제외한다
# 가지고 온 문서들을 사용자에게 보여준다

print("heelo")
return get_recommendations_service(request, db)
2 changes: 0 additions & 2 deletions src/recommendations/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,3 @@ class BookmarkBase(BaseModel):
summary: Optional[str] = Field(default=None, title="summary", description="북마크한 페이지의 summary", example="마음의 소리")
created_at: Optional[datetime] = Field(default=None, title="created_at", description="북마크한 시간", example=datetime.now())

class RecommendationResponse(BaseModel):
recommended_pages: List[BookmarkBase]
Loading

0 comments on commit 8bc2ae2

Please sign in to comment.