# 插入一筆資料
from dotenv import load_dotenv
import os
from pymongo import MongoClient
load_dotenv()
endpoint = os.environ["mongo_endpoint"]
mongo_client = MongoClient(endpoint)
collection = mongo_client["mydatabase"]["mycollection"]
data = {
"text":"Curry is MVP!!!",
"embedding": [0.123, 0.234, 0.345],
}
collection.insert_one(data)
成功寫入囉!!
InsertOneResult(ObjectId('66b89520a949ac25a8082508'), acknowledged=True)
from langchain_openai import AzureChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
from dotenv import load_dotenv
import os
chat_model = AzureChatOpenAI(
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
azure_deployment="gpt-4o",
openai_api_version="2023-03-15-preview",
)
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
str_parser = StrOutputParser()
test_prompt = ChatPromptTemplate.from_messages(
[
("system","你會回答所有問題"),
("human","{input}"),
])
test_chain = test_prompt | chat_model | str_parser
question = input("請輸入問題:")
print('question:', question)
response = test_chain.invoke({"input":question})
print('response:', response)
data = {
'user_query': question,
'ai_response': response
}
collection.insert_one(data)
question:
response: 我會盡力回答你的問題。如果你有任何問題或需要幫助,請隨時告訴我!
InsertOneResult(ObjectId('66b88f7ba949ac25a80824ec'), acknowledged=True)
成功寫入MongoDB囉!!
那我們要如何連接前面所學的
將有memory的問答功能寫入到我們的DB中呢
下一章將來探討!