|
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;letter-spacing: 0.1em;color: rgb(63, 63, 63);transition: 0.3s;">研究表明,GPT完成模型在国际象棋领域的表现相当出色,例如gpt-3.5-turbo-instruct的Elo评级大约为1800。 
然而,聊天模型的表现通常较差。已有研究表明,结合微调和提示优化能够显著提升模型性能。 ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;letter-spacing: 0.1em;color: rgb(63, 63, 63);transition: 0.3s;">为探讨能否将聊天模型的表现提升至与完成模型竞争的水平,进行了多次实验。ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;letter-spacing: 0.1em;color: rgb(63, 63, 63);transition: 0.3s;">本文采用DSPy进行提示优化,并通过微调gpt-4o-mini,以增强大型语言模型(LLM)在国际象棋难题中的解决能力。ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;padding-left: 12px;color: rgb(63, 63, 63);transition: 0.3s;">名词解释ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;color: rgb(63, 63, 63);" class="list-paddingleft-1">•微调Finetuning:在特定数据集上进一步训练预训练的语言模型,以提高其在特定任务上的表现。 •提示优化Prompt optimization:通过改进输入提示来引导 AI 模型,以获得更好和更准确的响应。 •少样本提示Few shot prompting:一种方法,其中语言模型在提示中提供少量示例(通常是 2-5 个),以帮助其理解和执行特定任务。 •思维链 (Chain of thought,CoT):一种鼓励 AI 模型将复杂问题分解为逐步推理的策略,通常在提示或训练数据中包含中间步骤。 •DSPy:用于算法优化 LM 提示和权重的高级框架。 •Completion Models:适用于明确、单一的任务,能够快速生成所需内容。它们适合于任务导向的应用,如问答系统和代码生成。比如GPT-3.5-turbo-instruct,Codex等 •Chat Models:设计用于与用户进行长时间的对话,能够处理多轮对话并维持上下文。它们适合于需要连续互动和自然对话的场景,如虚拟助理和客服系统,GPT-4、GPT-3.5-turbo等 ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;display: table;padding: 0.3em 1em;background: rgb(15, 76, 129);color: rgb(255, 255, 255);border-radius: 8px;box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 6px;transition: 0.3s;">数据集ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;letter-spacing: 0.1em;color: rgb(63, 63, 63);transition: 0.3s;">为了创建评估模型的数据集,我使用了 Lichess 数据库中的国际象棋难题集,该数据库提供了 400 万个带有元数据的国际象棋难题。ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;letter-spacing: 0.1em;color: rgb(63, 63, 63);transition: 0.3s;">对于每个难题,我通过 API 获取了每个难题的 PGN 格式的完整走棋列表,每个走棋都有一个示例。ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-feature-settings: normal;font-variation-settings: normal;font-size: 14px;margin: 10px 8px;color: rgb(221, 221, 221);background: rgb(48, 48, 48);letter-spacing: normal;text-align: left;line-height: 1.5;overflow-x: auto;border-radius: 8px;box-shadow: rgba(0, 0, 0, 0.05) 0px 0px 10px inset;transition: 0.3s;">PGN:1.e3e52.Ne2d53.Ng3Nf64.Nh5Bd65.d3Bg46. Answer:?ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 16px;letter-spacing: 0.1em;color: rgb(63, 63, 63);transition: 0.3s;">处理后的数据集可在 Hugging Face 上获取。
完成模型能解决国际象棋难题吗?GPT 完成模型提供了一个强有力的基准,babbage 和 davinci 分别在评估集中达到了61.23%和74.45%的准确率。 | 模型 | 准确率 | | babbage-002 | 61.23% | | davinci-002 | 74.45% |
提示优化在没有向 LLM 展示任何示例的情况下,聊天模型的表现相当差,gpt-4o-mini 仅解决了 17.4% 的难题,而 gpt-4o 解决了 28.44%。 DSPy 程序DSPy:一个用于编程(而不是提示)基础模型的框架
为了使用DSPy优化程序,可以通过LLM签名和模块来表达它。这个简单的签名将被优化为高质量的少样本提示。 仅通过这种方法,gpt-4o-mini的准确率提高到了25.99%,比零样本基线提高了50%。 当结合非法走棋的自我修正步骤和2次重试时,准确率进一步提高到了 31.72%。 classChessSolver(dspy.Signature): """GivenaseriesofchessmovesinPortableGameNotation(PGN)format,yourtaskistodetermineandreturnthecorrectnextmoveinStandardAlgebraicNotation(SAN)format.""" pgn=dspy.InputField(desc="Thechessposition") answer=dspy.OutputField(desc="ThecorrectnextmoveinSANformat")
classChessEngine(dspy.Module): def__init__(self): super().__init__() self.generate_move=dspy.ChainOfThought(ChessSolver)
defforward(self,pgn): gen_pred=self.generate_move(pgn=pgn) gen_move=gen_pred.answer gen_move=gen_move.split("")[-1] valid,reason=validate_pgn_move(pgn,gen_move) dspy.Suggest(valid,reason) ifvalid: print(f"valid:\n{pgn}*{gen_move}") ifnotvalid: print(f"invalid:\n{pgn}*{gen_move}*\n{reason}")
returndspy.Prediction(pgn=pgn,answer=gen_move,rationale=gen_pred.rationale)
检查走棋是否有效:defvalidate_pgn_move(pgn_board,san_move): #CreateaboardfromthePGN board=chess.Board() pgn=io.StringIO(pgn_board) game=chess.pgn.read_game(pgn)
#ApplyallmovesfromthePGNtogetthecurrentboardstate formoveingame.mainline_moves(): board.push(move)
#Parsethenewmove try: print(str(san_move)) chess_move=board.parse_san(str(san_move)) exceptchess.InvalidMoveError: returnFalse,"Invalidmovenotation" exceptchess.IllegalMoveError: returnFalse,"Illegalmove" exceptchess.AmbiguousMoveError: returnFalse,"SANisambigious"
#Checkifthemoveislegal ifchess_moveinboard.legal_moves: returnTrue,"Moveisvalid" else: returnFalse,"Moveisnotlegalinthecurrentposition"
通过使用DSPy对少样本提示进行优化,gpt-4o-mini的性能提升到了 31.72%。 使用新优化的少样本思维链提示结合gpt-4o,准确率提高到了 63.88%。 编译#定义超参数: N=20#Thenumberofinstructionsandfewshotexamplesthatwewillgenerateandoptimizeover batches=50#Thenumberofoptimizationtrialstoberun(wewilltestoutanewcombinationofinstructionsandfewshotexamplesineachtrial) temperature=1.0#Thetemperatureconfiguredforgeneratingnewinstructions
#设置评估指标 NUM_THREADS=64
#评估 metric=dspy.evaluate.answer_exact_match kwargs=dict(num_threads=NUM_THREADS,display_progress=True) evaluate=Evaluate(devset=val,metric=metric,**kwargs)
#基线 baseline_val_score=evaluate(program_with_assertions,devset=val) print(f"Baselineval:{baseline_val_score}")
#编译 eval_kwargs=dict(num_threads=NUM_THREADS,display_progress=True,display_table=0) teleprompter=MIPROv2(prompt_model=prompt_model,task_model=task_model,metric=metric,num_candidates=N,init_temperature=temperature,verbose=True) compiled_program=teleprompter.compile(program_with_assertions,trainset=train,valset=val,num_batches=batches,max_bootstrapped_demos=3,max_labeled_demos=5,eval_kwargs=eval_kwargs) compiled_program.save("compiled_chess_cot_ft_student.dspy")
#编译得分 fs_val_score,fs_outputs=evaluate(compiled_program,devset=val,return_outputs=True) print(f"Fewshotcompiledval:{fs_val_score}")
消耗数百万个Token后... 编译后的提示DSPy优化后的少样本思维链提示,包含 5 个示例: { "generate_move":{ "lm":null, "traces":[], "train":[], "demos":[ { "augmented":true, "pgn":"1.e4e52.Ne2Bc53.Ng3d64.Bc4h65.O-ONf66.d3O-O7.Nh5Bg48.Nxf6+Qxf69.", "rationale":"Pgn:1.e4e52.Ne2Bc53.Ng3d64.Bc4h65.O-ONf66.d3O-O7.Nh5Bg48.Nxf6+Qxf69.\nReasoning et'sthinkstepbystepinordertodeterminethebestmoveforWhite.After8...Qxf6,Blackhascapturedtheknightonf6,andnowWhiteneedstorespond.ThemostlogicalmoveforWhiteistoplay9.Qxg4,capturingthebishopong4.ThismovenotonlygainsmaterialbutalsoputspressureonBlack'sposition.", "move":"Qxg4" }, { "augmented":true, "pgn":"1.e4d52.exd5Qxd53.Nc3Qd84.Nf3Nf65.h3Bf56.Bc4e67.d3Be78.Qe2O-O9.Bd2a610.g4Bg611.Ne5b512.Bb3c513.h4h614.Nxg6fxg615.g5Nd516.Nxd5exd517.Qe6+Kh718.Bxd5Bxg519.O-O-OBxd2+20.Rxd2Rf621.Qe4Ra722.h5Re723.hxg6+Kh824.Qg2Qd625.Re2Rxe226.Rxh6+gxh627.g7+Kh728.", "rationale":"determinethebestmoveforWhite.Thepositionafter28.iscriticalasWhitehasastronginitiativeandisthreateningtodelivercheckmateorwinmaterial.ThelastmoveplayedbyBlackwas27...Kh7,whichputstheBlackkinginaprecariousposition.\n\nWhitehasseveraloptionstoconsider,butthemosteffectivemoveistoplay29.g8=Q+.Thismovepromotesthepawnong7toaqueen,deliveringchecktotheBlackking.Thenewlypromotedqueenwillalsocreateasignificantthreat,asitcanpotentiallyleadtocheckmateonthenextmoveifBlackdoesnotrespondadequately.", "move":"g8=Q+" }, { "pgn":"1.e4e52.Nf3Nc63.Bc4Bc54.c3Nf65.d4exd46.cxd4Bb4+7.Nc3Nxe48.O-OBxc39.d5Bf610.Re1Ne711.Rxe4d612.Bg5Bxg513.Nxg5h614.Nf3O-O15.Qe2Ng616.Re1Bf517.Rd4a618.Bd3Bxd319.Qxd3Qd720.h4Rae821.Rxe8Rxe822.h5Ne523.Nxe5Rxe524.g4Qe725.Kg2Re126.Qf5g627.hxg6fxg628.Qxg6+Qg729.Qxg7+Kxg730.Rd2Kf631.f4Re432.Kf3Rc433.b3Rc534.Ke4Ra535.a4Rc536.Rd3Rc137.Rh3Kg638.f5+Kg539.Kf3Rc3+40.Kg2", "answer":"Rxh3" }, { "pgn":"1.e4e52.f4exf43.Bc4d64.Nc3h65.d4g56.h4Bg77.hxg5hxg58.Rxh8Bxh89.Qh5Qf610.Nd5Qxd411.Nxc7+Kd812.Nf3Qxe4+13.Be2Kxc714.Qxh8Ne715.Nxg5Qxg216.Bxf4Bg417.O-O-OQxe218.Bxd6+Kb619.Qd4+Kc620.", "answer":"Qc3+" }, { "pgn":"1.e4e52.d3Nc63.Be2d54.exd5Qxd55.Bf3Qd86.Bxc6+bxc67.Nf3Bd68.O-Oh69.Qe2Qf610.d4Bg411.dxe5Bxf312.exf6+Bxe213.Re1Nxf614.Rxe2+Be715.b3O-O-O16.Rxe7", "answer":"Rd1+" } ], "signature_instructions":"GivenasequenceofchessmovesinPortableGameNotation(PGN)format,criticallyanalyzethecurrentboardpositiontodeterminethenextoptimalmoveinStandardAlgebraicNotation(SAN)format.Youranswershouldincludeadetailedstep-by-stepreasoningtojustifywhythismoveisthebestchoice,consideringthecurrentthreats,opportunitiesformaterialgain,andpositionaladvantages.Ensurethatyourrationaleexplainshowyourchosenmovealignswithanoverallstrategicplantoimproveyourpositionandcounteryouropponent'sthreats.", "signature_prefix":"Move:" } }
有趣的是,第一个示例中的基本原理存在错误,并重复了说明中的 PGN。删除它实际上会显着降低性能。 DSPy 编译结果
| 模型 | 准确率 | | gpt-4o-mini [zero shot] | 17.4% | | gpt-4o-mini | 25.99% | | gpt-4o-mini [SELF CORRECT 3 tries max] | 31.72% | | gpt-4o [zero shot] | 28.44% | | gpt-4o | 55.07% | | gpt-4o [SELF CORRECT 3 tries max] | 63.88% |
微调可以在这些数据上微调一个模型,而不是构建提示词。GPT-3.5-turbo-16k通常在LLM数据上表现良好,因此使用了gpt-4o-mini进行微调。 构建良好示例为了微调,需要创建高质量的训练示例。尽管原始Lichess数据库中有许多国际象棋难题,但并不是所有难题都适合用来训练模型。因此,手动选择了最适合微调的难题,并确保它们代表了各种不同的情况。这些例子包含了不同类型的局面,如开局、中局和残局。 每个示例都包括完整的棋局(PGN 格式)和正确的下一步(SAN 格式),并增加了详细的推理步骤,以帮助模型理解为什么这是最优解。 gpt-4o-minigpt-4o-mini 是一个相对较小的模型,在微调前的基线性能仅为 17.4%。在进行微调后,模型的表现显著提升,达到了 45.21%。虽然这仍不如提示优化后的 gpt-4o,但已经展示了微调对模型性能的有效性。 gpt-4ogpt-4o 是一个更大的模型,在未微调时的基线性能为 28.44%。通过 DSPy 编译的少样本提示优化,该模型的性能提升至 63.88%。在微调之后,gpt-4o 的性能进一步提升,达到了 70.32%,显示出微调的巨大潜力。 davinci为了比较,还尝试微调了 davinci-002,这是一个更强大的模型。davinci-002 在基线上的性能为 74.45%。经过微调后,模型的准确率达到了 80.12%。尽管 davinci-002 的基线性能已经很高,但微调仍然能够进一步提升其表现。 微调结果
完成模型| 模型 | 微调前准确率 | 微调后准确率 | | gpt-4o-mini | 17.4% | 45.21% | | gpt-4o | 28.44% | 70.32% | | davinci-002 | 74.45% | 80.12% |
聊天模型 + DSPy| 模型 | 准确度 | | gpt-4o-mini [zero shot] | 17.4% | | gpt-4o-mini | 25.99% | | gpt-4o-mini [SELF CORRECT 3 tries max] | 31.72% | | gpt-4o-mini finetune | 57.71% | | gpt-4o-mini finetune [SELF CORRECT 3 tries max] | 65.64% | | gpt-4o [zero shot] | 28.44% | | gpt-4o | 55.07% | | gpt-4o [SELF CORRECT 3 tries max] | 63.88% | | gpt-4o finetune | 58.59% | | gpt-4o finetune [SELF CORRECT 3 tries max] | 71.37% |
注意事项尽管实验结果表明,DSPy 编译的少样本提示和微调可以显著提升聊天模型的国际象棋难题解决能力。 但仍有一些注意事项: 1、微调过程需要大量的数据和计算资源。 2、不同类型的国际象棋难题可能需要不同的优化策略,这意味着我们可能需要针对特定类型的难题进行额外的优化。 总结本研究展示了通过DSPy提示优化和微调,可以显著提升聊天模型解决国际象棋难题的能力。 在最好的情况下,gpt-4o在经过DSPy提示优化和微调后,达到了70.32%的准确率,接近于完成模型的表现。 然而,仍有一些差距需要弥补,未来的工作将继续探索如何进一步优化这些模型,以提高其在各种复杂局面中的表现。 |