{"id":205401,"date":"2025-05-29T08:17:35","date_gmt":"2025-05-29T00:17:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205401\/"},"modified":"2025-05-29T08:17:35","modified_gmt":"2025-05-29T00:17:35","slug":"dspy%ef%bc%9a%e8%af%ad%e8%a8%80%e6%a8%a1%e5%9e%8b%e7%bc%96%e7%a8%8b%e7%9a%84%e6%96%b0%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205401\/","title":{"rendered":"DSPy\uff1a\u8bed\u8a00\u6a21\u578b\u7f16\u7a0b\u7684\u65b0\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>DSPy\uff1a\u8bed\u8a00\u6a21\u578b\u7f16\u7a0b\u7684\u65b0\u65b9\u6cd5<\/h1>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aDSPy\uff1a\u8bed\u8a00\u6a21\u578b\u7f16\u7a0b\u7684\u65b0\u65b9\u6cd5\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241130\/1732977765674b24656ea29.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5728\u4f7f\u7528\u8bed\u8a00\u6a21\u578b (llm) \u65f6\uff0c\u5f00\u53d1\u4eba\u5458\u9762\u4e34\u7740\u4e00\u7cfb\u5217\u5e38\u89c1\u7684\u6311\u6218\u3002\u6211\u4eec\u82b1\u8d39\u4e86\u65e0\u6570\u7684\u65f6\u95f4\u6765\u5236\u4f5c\u5b8c\u7f8e\u7684\u63d0\u793a\uff0c\u5374\u53d1\u73b0\u5f53\u6211\u4eec\u5207\u6362\u6a21\u578b\u6216\u8f93\u5165\u7565\u6709\u53d8\u5316\u65f6\uff0c\u6211\u4eec\u7cbe\u5fc3\u8bbe\u8ba1\u7684\u89e3\u51b3\u65b9\u6848\u5c31\u4f1a\u5d29\u6e83\u3002\u4f20\u7edf\u7684\u5373\u65f6\u5de5\u7a0b\u65b9\u6cd5\u662f\u624b\u52a8\u7684\u3001\u8017\u65f6\u7684\uff0c\u800c\u4e14\u901a\u5e38\u662f\u4e0d\u53ef\u9884\u6d4b\u7684\u3002<\/p>\n<p>dspy\uff08\u58f0\u660e\u5f0f\u81ea\u6211\u6539\u8fdb python\uff09\u7684\u51fa\u73b0\u662f\u65af\u5766\u798f nlp \u5bf9\u8fd9\u4e9b\u6311\u6218\u7684\u56de\u7b54\u3002\u6b63\u5982\u4ed6\u4eec\u7684\u7f51\u7ad9 (dspy.ai) \u6240\u63cf\u8ff0\u7684\uff0c\u5b83\u662f\u201c\u7528\u4e8e\u7f16\u7a0b\uff08\u800c\u4e0d\u662f\u63d0\u793a\uff09\u8bed\u8a00\u6a21\u578b\u7684\u5f00\u6e90\u6846\u67b6\u201d\u3002\u5b83\u652f\u6301\u6784\u5efa\u6a21\u5757\u5316 ai \u7cfb\u7edf\u7684\u5feb\u901f\u8fed\u4ee3\uff0c\u5e76\u63d0\u4f9b\u7528\u4e8e\u4f18\u5316\u63d0\u793a\u548c\u6743\u91cd\u7684\u7b97\u6cd5\uff0c\u65e0\u8bba\u60a8\u662f\u6784\u5efa\u7b80\u5355\u7684\u5206\u7c7b\u5668\u3001\u590d\u6742\u7684 rag \u7ba1\u9053\u8fd8\u662f\u4ee3\u7406\u5faa\u73af\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5b89\u88c5\u6846\u67b6\uff1a<\/p>\n<pre>pip install -u dspy\n\nimport dspy\nlm = dspy.lm('openai\/gpt-4-mini', api_key='your_openai_api_key')\ndspy.configure(lm=lm)\n<\/pre>\n<p>\u7b7e\u540d\u662f dspy \u58f0\u660e\u5f0f\u65b9\u6cd5\u7684\u57fa\u7840\u3002\u4ed6\u4eec\u4ee5\u7b80\u5355\u7684\u683c\u5f0f\u5b9a\u4e49\u8f93\u5165\u548c\u8f93\u51fa\u7684\u8bed\u4e49\u89d2\u8272\uff1a<\/p>\n<pre># simple question answering\n\"question -&gt; answer\"\n\n# retrieval-based qa\n\"context: list[str], question: str -&gt; answer: str\"\n\n# multiple-choice with reasoning\n\"question, choices: list[str] -&gt; reasoning: str, selection: int\"\n<\/pre>\n<p>dspy \u4e3a\u4e0d\u540c\u7684\u7528\u4f8b\u63d0\u4f9b\u4e86\u51e0\u4e2a\u5173\u952e\u6a21\u5757\uff1a<\/p>\n<ul>\n<li> \u9884\u6d4b\uff1a\u76f4\u63a5 llm \u56de\u590d<\/li>\n<li> chainofthought\uff1a\u9010\u6b65\u63a8\u7406<\/li>\n<li> programofthought\uff1a\u57fa\u4e8e\u4ee3\u7801\u7684\u89e3\u51b3\u65b9\u6848<\/li>\n<li> react\uff1a\u57fa\u4e8e\u4ee3\u7406\u7684\u4ea4\u4e92<\/li>\n<li> multichaincomparison\uff1a\u6bd4\u8f83\u591a\u4e2a\u63a8\u7406\u8def\u5f84<\/li>\n<\/ul>\n<h4> \u6570\u5b66\u95ee\u9898\u89e3\u51b3 <\/h4>\n<pre>math = dspy.chainofthought(\"question -&gt; answer: float\")\nmath(question=\"two dice are tossed. what is the probability that the sum equals two?\")\n<\/pre>\n<h4> \u68c0\u7d22\u589e\u5f3a\u751f\u6210 (rag) <\/h4>\n<pre>def search_wikipedia(query: str) -&gt; list[str]:\n    results = dspy.ColBERTv2(url='http:\/\/20.102.90.50:2017\/wiki17_abstracts')(query, k=3)\n    return [x['text'] for x in results]\n\nrag = dspy.ChainOfThought('context, question -&gt; response')\n<\/pre>\n<p>dspy \u652f\u6301\u5404\u79cd\u9ad8\u7ea7\u7528\u4f8b\uff1a<\/p>\n<ul>\n<li>\u5206\u7c7b\u4efb\u52a1<\/li>\n<li>\u4fe1\u606f\u63d0\u53d6<\/li>\n<li>\u5e26\u6709\u5de5\u5177\u7684\u57fa\u4e8e\u4ee3\u7406\u7684\u7cfb\u7edf<\/li>\n<li>\u590d\u6742\u7684 rag \u7ba1\u9053<\/li>\n<\/ul>\n<p>\u6846\u67b6\u7684\u81ea\u6211\u6539\u8fdb\u7279\u6027\u610f\u5473\u7740\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\u4f18\u5316\u5176\u6027\u80fd\uff0c\u4ece\u4ea4\u4e92\u548c\u7ed3\u679c\u4e2d\u5b66\u4e60\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u5728 dspy \u6587\u6863\u548c\u793e\u533a\u5b58\u50a8\u5e93\u4e2d\u627e\u5230\u5b8c\u6574\u7684\u793a\u4f8b\u5e76\u63a2\u7d22\u66f4\u591a\u7528\u4f8b\uff0c\u7f51\u5740\u4e3a https:\/\/github.com\/gabrielvanderlei\/dspy-examples.<\/p>\n<p>dspy \u4ee3\u8868\u4e86\u4ece\u4f20\u7edf\u7684\u5373\u65f6\u5de5\u7a0b\u5230\u4f7f\u7528\u8bed\u8a00\u6a21\u578b\u7684\u58f0\u660e\u5f0f\u7f16\u7a0b\u7684\u8303\u5f0f\u8f6c\u53d8\u3002\u5b83\u4e3a\u6cd5\u5b66\u7855\u58eb\u5f00\u53d1\u5e26\u6765\u4e86\u7ed3\u6784\u3001\u53ef\u9760\u6027\u548c\u53ef\u9884\u6d4b\u6027\uff0c\u4f7f\u6784\u5efa\u548c\u7ef4\u62a4\u4eba\u5de5\u667a\u80fd\u9a71\u52a8\u7684\u5e94\u7528\u7a0b\u5e8f\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aDSPy\uff1a\u8bed\u8a00\u6a21\u578b\u7f16\u7a0b\u7684\u65b0\u65b9\u6cd5\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DSPy\uff1a\u8bed\u8a00\u6a21\u578b\u7f16\u7a0b\u7684\u65b0\u65b9\u6cd5 &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205401","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205401"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205401\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}