Skip to content

Commit

Permalink
#77 feat: title과 review를 embedding해서 pinecone index에 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
wjdwlghks committed May 15, 2024
1 parent f8ec014 commit 2ed404a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class Book {
private Long id;
private String isbn;
private String title;
private String description;
private String author;
private String publisher;
@Column(name = "publish_date")
Expand All @@ -52,7 +51,7 @@ public class Book {
private List<MyBook> membersAddThisBook = new ArrayList<>();

public Book(AddBookRequest request) {
this(null, request.isbn(), request.title(), request.description(), request.author(), request.publisher(), request.publishDate(),
this(null, request.isbn(), request.title(), request.author(), request.publisher(), request.publishDate(),
request.imageUrl(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
}
}
3 changes: 3 additions & 0 deletions fastapi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.env
__pycache__
58 changes: 58 additions & 0 deletions fastapi/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os
from fastapi import FastAPI
from pydantic import BaseModel
from openai import OpenAI
from pinecone import Pinecone
from dotenv import load_dotenv

app = FastAPI()
load_dotenv()
class EmbeddingRequest(BaseModel):
isbn: str
title: str
description: str

@app.post("/embed")
async def get_embedding(embedding_request: EmbeddingRequest):
isbn = embedding_request.isbn
title = embedding_request.title
review = embedding_request.description

# using openai embedding model
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
response = client.embeddings.create(
input=title,
model="text-embedding-3-small"
)
title_embedding = response.data[0].embedding

response = client.embeddings.create(
input=review,
model="text-embedding-3-small"
)
review_embedding = response.data[0].embedding

# upsert to pinecone index
pc = Pinecone(api_key=os.environ.get("PINECONE_API_KEY"))
index = pc.Index("review-and-title-embedding")
index.upsert(
vectors=[
{
"id": isbn,
"values": title_embedding,
"metadata": {"type": "title"}
}
],
namespace="title-embeddings"
)
index.upsert(
vectors=[
{
"id": isbn,
"values": review_embedding,
"metadata": {"type": "review"}
}
],
namespace="review-embeddings"
)
return "ok"

0 comments on commit 2ed404a

Please sign in to comment.