Spaces:
Running
Running
import gradio as gr | |
from langchain.embeddings.huggingface import HuggingFaceEmbeddings | |
from langchain.vectorstores import FAISS | |
from openai import OpenAI | |
YOUR_OPENAI_API_KEY = "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" | |
# ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋ | |
embedding_model_name = "snunlp/KR-SBERT-V40K-klueNLI-augSTS" | |
embedding_model = HuggingFaceEmbeddings( | |
model_name=embedding_model_name, | |
encode_kwargs={"normalize_embeddings": True}, | |
) | |
# vector DB ๋ก๋๋ | |
save_path = "./my_faiss_index" | |
vectorstore = FAISS.load_local(save_path, embedding_model,allow_dangerous_deserialization=True) | |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3}) | |
def chatbot(input_question): | |
basic_docs = retriever.invoke(input_question) | |
context = "\n".join([doc.page_content for doc in basic_docs]) | |
client = OpenAI( | |
api_key=YOUR_OPENAI_API_KEY) | |
# question๊ณผ context๋ฅผ message์ ์ฝ์ | |
completion = client.chat.completions.create( | |
model="gpt-4o-mini", | |
messages=[ | |
{ | |
"role": "user", | |
"content": f"""๋น์ ์ ๋ฐ๋์ฒด์ ์ฐจ์ธ๋๋ฐ๋์ฒดํ๊ณผ์ ๋ํด์ ์ค๋ช ํ๋ Assistant์ ๋๋ค. | |
์ฐจ์ธ๋๋ฐ๋์ฒดํ๊ณผ๋ ํ๊ตญ ์์ธ์ ์ค์๋ํ๊ต์ ์ฐฝ์ICT๊ณต๊ณผ๋ํ์ ์ค๋ฆฝ๋ ํ๊ณผ์ ๋๋ค. | |
๋ค์ ๋งฅ๋ฝ์ ๋ง๊ฒ ์ง๋ฌธ์ ๋ตํ์ธ์. | |
๋งฅ๋ฝ: {context} | |
์ง๋ฌธ: {input_question} | |
""" | |
} | |
] | |
) | |
return completion.choices[0].message.content | |
demo = gr.Interface(fn=chatbot, inputs="text", outputs="text") | |
demo.launch() | |