iT邦幫忙

2023 iThome 鐵人賽

DAY 1
0

今日題目

https://ithelp.ithome.com.tw/upload/images/20230905/20153007oKpRYqLKEL.png


實作

  1. 創建system prompt

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}")
    ]
)

  1. 選擇memory記憶方式
memory = ConversationBufferMemory(memory_key="chat_history",return_messages=True)

  1. 建立conversation LLM chain,選擇GPT-4的模型
llm = ChatOpenAI(temperature=0,openai_api_key=OPENAI_API_KEY,model="gpt-4")

conversation = LLMChain(
    llm=llm,
    prompt=prompt,
    verbose=True,
    memory=memory,
)
  1. 最後,送出訊息,期待AI的回答吧!!!
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)

成果

https://ithelp.ithome.com.tw/upload/images/20230905/20153007PwVjcFu3oK.png

[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= 不客氣!如果你有其他的程式設計挑戰或問題,隨時告訴我。祝你學習愉快!

上一篇
Day01 - Langchain Agent 實作虛擬旅行策劃師
下一篇
Day03 - Langchain 請假囉
系列文
LangChain鸚鵡學院:語言魔法師拿起上AI魔杖,工程師的新時代即將開展4
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
finnohh
iT邦新手 5 級 ‧ 2023-09-20 17:35:27

請問如何在LLMChain中加入自己建立的ChromaDB? 有參數可以將DB加入Chain中嗎?謝謝!
我的ChromaDB設計如下:
from langchain.vectorstores import Chroma

vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding)

我要留言

立即登入留言