forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#77 feat: title과 review를 embedding해서 pinecone index에 저장
- Loading branch information
Showing
3 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
.env | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |