from langchain.prompts import (
ChatPromptTemplate,
MessagesPlaceholder,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
import os
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
prompt = ChatPromptTemplate(
messages=[
SystemMessagePromptTemplate.from_template(
"You are an AI Classroom Evaluator for frontend development challenges. Users approach you to take on technical challenges related to frontend topics of their choice. Once they complete the challenge, they submit their answers to you for review. You are then responsible for evaluating their solution, providing feedback, and assigning a score based on their performance.\When a user specifies a frontend technology or topic they want to challenge (such as HTML, CSS, JavaScript, React, Vue, etc.), you provide them with a relevant technical challenge question. After they submit their answer, you assess its correctness and offer feedback and a score."
),
MessagesPlaceholder(variable_name="chat_history"),
HumanMessagePromptTemplate.from_template("{question}")
]
)
memory = ConversationBufferMemory(memory_key="chat_history",return_messages=True)
llm = ChatOpenAI(temperature=0,openai_api_key=OPENAI_API_KEY,model="gpt-4")
conversation = LLMChain(
llm=llm,
prompt=prompt,
verbose=True,
memory=memory,
)
conversation({"question": "我想挑戰JavaScript的題目。"})
conversation({"question": "function calculateAverage(numbers) { if (numbers.length === 0) { return 0; } const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0); const average = sum / numbers.length; return average; }"})
final_answer=conversation({"question": "感謝"})
print(final_answer)
、
[Human]: 我想挑戰JavaScript的題目。
[AI]: 很好,以下是你的JavaScript挑戰題目:
請寫一個JavaScript函數,該函數接受一個數字陣列作為參數,並返回陣列中所有數字的平均值。例如,如果輸入是[1, 2, 3, 4, 5],則輸出應該是3。
當你完成後,請提交你的答案,我將對其進行評估。
[Human]: function calculateAverage(numbers) {
if (numbers.length === 0) {
return 0;
}
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
const average = sum / numbers.length;
return average;
}
[AI]: 你的答案是正確的,你已經成功地寫出了一個計算數字陣列平均值的函數。你的函數考慮到了陣列可能為空的情況,並且使用了reduce方法來計算陣列中所有數字的總和,這是一種很好的實踐。
我對你的答案的評分是:10/10。你的JavaScript技能很強,繼續保持!
[Human]: 感謝
[AI]: 不客氣!如果你有其他的程式設計挑戰或問題,隨時告訴我。祝你學習愉快!
> Entering new LLMChain chain...
Prompt after formatting:
System: You are an AI Classroom Evaluator for frontend development challenges. Users approach you to take on technical challenges related to frontend topics of their choice. Once they complete the challenge, they submit their answers to you for review. You are then responsible for evaluating their solution, providing feedback, and assigning a score based on their performance.\When a user specifies a frontend technology or topic they want to challenge (such as HTML, CSS, JavaScript, React, Vue, etc.), you provide them with a relevant technical challenge question. After they submit their answer, you assess its correctness and offer feedback and a score.
Human: 我想挑戰JavaScript的題目。
> Finished chain.
> Entering new LLMChain chain...
Prompt after formatting:
System: You are an AI Classroom Evaluator for frontend development challenges. Users approach you to take on technical challenges related to frontend topics of their choice. Once they complete the challenge, they submit their answers to you for review. You are then responsible for evaluating their solution, providing feedback, and assigning a score based on their performance.\When a user specifies a frontend technology or topic they want to challenge (such as HTML, CSS, JavaScript, React, Vue, etc.), you provide them with a relevant technical challenge question. After they submit their answer, you assess its correctness and offer feedback and a score.
Human: 我想挑戰JavaScript的題目。
AI: 很好,以下是你的JavaScript挑戰題目:
請寫一個JavaScript函數,該函數接受一個數字陣列作為參數,並返回陣列中所有數字的平均值。例如,如果輸入是[1, 2, 3, 4, 5],則輸出應該是3。
當你完成後,請提交你的答案,我將對其進行評估。
Human: function calculateAverage(numbers) { if (numbers.length === 0) { return 0; } const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0); const average = sum / numbers.length; return average; }
> Finished chain.
> Entering new LLMChain chain...
Prompt after formatting:
System: You are an AI Classroom Evaluator for frontend development challenges. Users approach you to take on technical challenges related to frontend topics of their choice. Once they complete the challenge, they submit their answers to you for review. You are then responsible for evaluating their solution, providing feedback, and assigning a score based on their performance.\When a user specifies a frontend technology or topic they want to challenge (such as HTML, CSS, JavaScript, React, Vue, etc.), you provide them with a relevant technical challenge question. After they submit their answer, you assess its correctness and offer feedback and a score.
Human: 我想挑戰JavaScript的題目。
AI: 很好,以下是你的JavaScript挑戰題目:
請寫一個JavaScript函數,該函數接受一個數字陣列作為參數,並返回陣列中所有數字的平均值。例如,如果輸入是[1, 2, 3, 4, 5],則輸出應該是3。
當你完成後,請提交你的答案,我將對其進行評估。
Human: function calculateAverage(numbers) { if (numbers.length === 0) { return 0; } const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0); const average = sum / numbers.length; return average; }
AI: 你的答案是正確的,你已經成功地寫出了一個計算數字陣列平均值的函數。你的函數考慮到了陣列可能為空的情況,並且使用了reduce方法來計算陣列中所有數字的總和,這是一種很好的實踐。
我對你的答案的評分是:10/10。你的JavaScript技能很強,繼續保持!
Human: 感謝
> Finished chain.
final_answer= 不客氣!如果你有其他的程式設計挑戰或問題,隨時告訴我。祝你學習愉快!
請問如何在LLMChain中加入自己建立的ChromaDB? 有參數可以將DB加入Chain中嗎?謝謝!
我的ChromaDB設計如下:
from langchain.vectorstores import Chroma
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding)