import dspy
lm = dspy.LM("azure/gpt-4-32k",
api_key="xxx",
api_version="",
api_base="https://your-api-endpoint/ai/chat-ai/gpt4",)
dspy.configure(lm=lm)classNegativePositiveJudge(dspy.Signature):"""Judge if the answer is factually correct based on the context."""
context = dspy.InputField(desc="Context for the prediction")
question = dspy.InputField(desc="Question to be answered")
answer = dspy.InputField(desc="Answer for the question")
factually_correct = dspy.OutputField(
desc="Is the answer factually correct based on the context?",
prefix="Factual[positive/negative]:",)
judge = dspy.ChainOfThought(NegativePositiveJudge)
factual = judge(
context="今日はとても気分がいいです",
question="contextで与えた文章がネガティブかポジティブか判定してください",
answer="positive",)# ポジティブな文章を与えて期待する答えもポジティブなので結果はyesになるprint(factual)
factual = judge(
context="今日はとても気分が悪いです、最悪の日です",
question="contextで与えた文章がネガティブかポジティブか判定してください",
answer="positive",)# ネガティブな文章を与えて期待する答えがポジティブなので結果はnoになるprint(factual)classClassifier(dspy.Signature):
context = dspy.InputField(desc="Context for the prediction")
question = dspy.InputField(desc="Question to be answered")
factually_correct = dspy.OutputField(
desc="Classify a word or sentence passed in context. There are three categories: 'Fruits', 'Toys' and 'Others.'",)
classifier = dspy.ChainOfThought(Classifier)
factual = classifier(
context="みかん",
question="contextで与えた文章を分類してください。",)print(factual)
factual = classifier(
context="餃子",
question="contextで与えた文章を分類してください。",)print(factual)
factual = classifier(
context="hawksnowlog",
question="contextで与えた文章を分類してください。",)print(factual)
factual = classifier(
context="ちいかわのぬいぐるみはかわいいです。",
question="contextで与えた文章を分類してください。",)print(factual)
factual = classifier(
context="冬はみかんがおいしいですねー。",
question="contextで与えた文章を分類してください。",)print(factual)
classNegativePositiveJudge(dspy.Signature):"""Judge if the answer is factually correct based on the context."""
context = dspy.InputField(desc="Context for the prediction")
question = dspy.InputField(desc="Question to be answered")
answer = dspy.InputField(desc="Answer for the question")
factually_correct = dspy.OutputField(
desc="Is the answer factually correct based on the context?",
prefix="Factual[positive/negative]:",)
judge = dspy.ChainOfThought(NegativePositiveJudge)
factual = judge(
context="今日はとても気分がいいです",
question="contextで与えた文章がネガティブかポジティブか判定してください",
answer="positive",)# ポジティブな文章を与えて期待する答えもポジティブなので結果はyesになるprint(factual)
factual = judge(
context="今日はとても気分が悪いです、最悪の日です",
question="contextで与えた文章がネガティブかポジティブか判定してください",
answer="positive",)# ネガティブな文章を与えて期待する答えがポジティブなので結果はnoになるprint(factual)
classClassifier(dspy.Signature):
context = dspy.InputField(desc="Context for the prediction")
question = dspy.InputField(desc="Question to be answered")
factually_correct = dspy.OutputField(
desc="Classify a word or sentence passed in context. There are three categories: 'Fruits', 'Toys' and 'Others.'",)
classifier = dspy.ChainOfThought(Classifier)
factual = classifier(
context="みかん",
question="contextで与えた文章を分類してください。",)print(factual)
factual = classifier(
context="餃子",
question="contextで与えた文章を分類してください。",)print(factual)
factual = classifier(
context="hawksnowlog",
question="contextで与えた文章を分類してください。",)print(factual)
factual = classifier(
context="ちいかわのぬいぐるみはかわいいです。",
question="contextで与えた文章を分類してください。",)print(factual)
factual = classifier(
context="冬はみかんがおいしいですねー。",
question="contextで与えた文章を分類してください。",)print(factual)
結果は以下の通りです、ちゃんと分類できていることがわかります
Prediction(
reasoning='The word "みかん" refers to a type of citrus fruit commonly known as a mandarin or tangerine in English. It is a type of fruit.',
factually_correct='Fruits')
Prediction(
reasoning='The word "餃子" refers to a type of food, specifically dumplings, which are not related to fruits or toys. Therefore, it does not fit into the categories of \'Fruits\' or \'Toys\'.',
factually_correct='Others')
Prediction(
reasoning='The word "hawksnowlog" does not fit into the categories of \'Fruits\' or \'Toys\'. It appears to be a unique or specific term that does not relate to common fruits or toys.',
factually_correct='Others')
Prediction(
reasoning='The context mentions "ちいかわのぬいぐるみ" which translates to "Chiikawa plush toy" in English. This indicates that the subject is a type of toy.',
factually_correct='Toys')
Prediction(
reasoning='The context mentions "みかん" which is a type of fruit. The sentence is discussing the taste of "みかん" in winter, which is related to the category of \'Fruits\'.',
factually_correct='Fruits')
# 日本語化classJapaneseQA(dspy.Signature):
question = dspy.InputField()
answer = dspy.OutputField(desc="The answer should be in Japanese")
generate_jp_answer = dspy.ChainOfThought(JapaneseQA)
result = generate_jp_answer(question="こんにちわ!あなたは誰ですか?")print(result)
ヒントありで質問
ヒントありで質問するとそのヒントをもとに回答を作成することができます
# ヒントあり質問
result = generate_jp_answer(
question="hawksnowlogがよく書くブログのカテゴリは何ですか?",
hint="hawksnowlogのぶログはこれです、https://hawksnowlog.blogspot.com/",)print(result)
pot = dspy.ProgramOfThought(JapaneseQA)
question ="サラはリンゴを5個持っています。彼女は店からさらに 7 個のリンゴを購入します。サラは今リンゴを何個持っていますか?"
result = pot(question=question)print(result)
colbertv2_wiki17_abstracts = dspy.ColBERTv2(
url="http://20.102.90.50:2017/wiki17_abstracts")
dspy.settings.configure(rm=colbertv2_wiki17_abstracts)
retriever = dspy.Retrieve(k=3)
query ="When was the first FIFA World Cup held?"
topK_passages = retriever(query).passages # type: ignorefor idx, passage inenumerate(topK_passages):print(f"{idx+1}]", passage,"\n")
import dspy
lm = dspy.LM("azure/gpt-4-32k",
api_key="xxx",
api_version="",
api_base="https://your-api-endpoint/ai/chat-ai/gpt4",)
dspy.configure(lm=lm)
result = lm("Say this is a test!", temperature=0.7)print(result)