{"id":5045,"date":"2024-04-24T11:59:10","date_gmt":"2024-04-24T03:59:10","guid":{"rendered":"http:\/\/xinblog.ltd\/?p=5045"},"modified":"2024-04-28T10:16:52","modified_gmt":"2024-04-28T02:16:52","slug":"2-langchain%e7%9a%84%e5%bf%ab%e9%80%9f%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=5045","title":{"rendered":"2. LangChain\u7684\u5feb\u901f\u5165\u95e8"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" width=\"865\" height=\"240\" src=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-2.png\" class=\"wp-image-5046\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-2.png 865w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-2-300x83.png 300w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-2-768x213.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/p>\n<p>LangChain\uff0c\u4e00\u4e2a\u7528\u4e8e\u5927\u6a21\u578b\u76f8\u5173\u5f00\u53d1\u7684\u6846\u67b6<\/p>\n<p>\u5176\u5c31\u50cf\u662f\u4e00\u4e2a\u4e50\u9ad8\u79ef\u6728\u4e00\u6837\uff0c\u53ef\u4ee5\u6309\u7167\u81ea\u5df1\u9700\u8981\u7684\u90e8\u5206\u5feb\u901f\u6784\u5efa\u9879\u76ee<\/p>\n<p>\u5176\u652f\u6301Python\u548cJava script\u4e24\u4e2a\u7248\u672c\uff0c\u6211\u4eec\u4e3b\u8981\u4ee5Python\u8fdb\u884c\u6f14\u793a<\/p>\n<p>\u9996\u5148\u662f\u5b89\u88c5<\/p>\n<p>\u5176\u5b89\u88c5\u76f8\u5bf9\u6bd4\u8f83\u7b80\u5355<\/p>\n<p>pip install langchain<\/p>\n<p>pip install \u2013upgrade langchain<\/p>\n<p>\u8fd9\u662f\u6700\u57fa\u672c\u7684\u4f9d\u8d56\u9879\uff0c\u5982\u679c\u9700\u8981\u8ddf\u4e0d\u540c\u7684\u6a21\u578b\u8fdb\u884c\u96c6\u6210\uff0c\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u5176\u4ed6\u4f9d\u8d56\u3002<\/p>\n<p>\u6bd4\u5982<\/p>\n<p>pip install openai\u7b49<\/p>\n<p>\u6216\u8005\u8003\u8651\u76f4\u63a5\u5b89\u88c5\u5b8c\u6574\u5305<\/p>\n<p>pip install langchain[llms]<\/p>\n<p>GitHub\u793e\u533a\u7684\u5730\u5740<\/p>\n<p><a href=\"https:\/\/github.com\/langchain-ai\/langchain\">https:\/\/github.com\/langchain-ai\/langchain<\/a><\/p>\n<p>LangChain\u4e5f\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684API\u6587\u6863<\/p>\n<p><a href=\"https:\/\/python.langchain.com\/docs\/get_started\">https:\/\/python.langchain.com\/docs\/get_started<\/a><\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u4ee5OpenAI \u4e3a\u4f8b\uff0c\u8fdb\u884c\u6f14\u793a<\/p>\n<p>\u4e0d\u8fc7\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5f97\u5230\u4e00\u4e2aAPI Key<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"644\" height=\"332\" src=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-3.png\" class=\"wp-image-5047\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-3.png 644w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-3-300x155.png 300w\" sizes=\"(max-width: 644px) 100vw, 644px\" \/><\/p>\n<p>\u800c\u5728GPT\u4e4b\u4e2d\uff0c\u4e3b\u8981\u5206\u4e3a\u4e24\u4e2a\u6a21\u578b\uff0c\u5206\u4e3a\u4e86Chat Model\uff0cText Model<\/p>\n<p>\u5206\u522b\u5bf9\u5e94\u4e86\u804a\u5929\u6a21\u578b\uff0c\u53ef\u4ee5\u4ea7\u751f\u4eba\u4e0eAI\u4e4b\u95f4\u7684\u5bf9\u8bdd<\/p>\n<p>\u5176\u6b21\u662f\u6587\u672c\u6a21\u578b\uff0c\u4e3b\u8981\u662f\u6839\u636e\u4e00\u4e2a\u4efb\u52a1\u8fdb\u884c\u76f4\u63a5\u8fd4\u56de<\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u5148\u770b\u4e0b\u8c03\u7528Text\u6a21\u578b\u7684\u6d41\u7a0b<\/p>\n<p>\u4e0d\u8fc7\u5728\u6b64\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u770b\u4e0bopenai key\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u5206\u4e3a\u4e86\u73af\u5883\u53d8\u91cf\u548c\u76f4\u63a5\u4f7f\u7528<\/p>\n<p>\u73af\u5883\u53d8\u91cf\u53ef\u4ee5\u4e3a<\/p>\n<p>import os<\/p>\n<p>os.environ[&#8220;OPENAI_API_KEY&#8221;] = &#8216;\u4f60\u7684Open API Key&#8217;<\/p>\n<p>export OPENAI_API_KEY=&#8217;\u4f60\u7684Open API Key&#8217;<\/p>\n<p>\u76f4\u63a5\u4f7f\u7528<\/p>\n<p>import openai<\/p>\n<p>openai.api_key = &#8216;\u4f60\u7684Open API Key&#8217;<\/p>\n<p>\u4e4b\u540e\u5c31\u662f\u5173\u4e8eText\u6a21\u578b\u7684\u4f7f\u7528<\/p>\n<p>import openai<\/p>\n<p>response = openai.Completion.create(\u00a0 model=&#8221;text-davinci-003&#8243;,\u00a0 temperature=0.5,\u00a0 max_tokens=100,\u00a0 prompt=&#8221;\u8bf7\u7ed9\u6211\u7684\u82b1\u5e97\u8d77\u4e2a\u540d&#8221;)<\/p>\n<p>\u7136\u540e\u4eceresponse\u4e2d\u8bb2\u7ed3\u679c\u8fdb\u884c\u53d6\u56de<\/p>\n<p>print(response.choices[0].text.strip())<\/p>\n<p>\u6211\u4eec\u4eceresponse\u4e2d\u8bb2\u7ed3\u679c\u53d6\u51fa<\/p>\n<p>\u5bf9\u4e8e\u8bf7\u6c42\uff0c\u5176\u4e2d\u5305\u542b\u5e38\u89c1\u7684\u53c2\u6570<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"908\" height=\"934\" src=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-4.png\" class=\"wp-image-5048\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-4.png 908w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-4-292x300.png 292w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-4-768x790.png 768w\" sizes=\"(max-width: 908px) 100vw, 908px\" \/><\/p>\n<p>\u5176\u4e2d\u7684\u5173\u952e\u5728\u4e8e<\/p>\n<p>prompt\u662f\u8bed\u8a00\u4f20\u5165<\/p>\n<p>model\u662f\u6307\u5b9a\u6a21\u578b<\/p>\n<p>\u7136\u540e\u662f\u54cd\u5e94\uff0c\u5176\u4e2d\u5305\u542b<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"865\" height=\"361\" src=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-5.png\" class=\"wp-image-5049\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-5.png 865w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-5-300x125.png 300w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2024\/04\/unnamed-file-5-768x321.png 768w\" sizes=\"(max-width: 865px) 100vw, 865px\" \/><\/p>\n<p>\u5176\u4e2d\u91cd\u70b9\u5728\u4e8echoices\uff0c\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6a21\u578b\u751f\u6210\u7684\u6240\u6709\u8f93\u51fa<\/p>\n<p>\u8fd9\u4e2a\u5217\u8868\u4e2d\u5305\u542b\u4e86\u591a\u4e2a\u5b57\u5178\uff0c\u6bcf\u4e2a\u5b57\u5178\u5305\u542b\u5982\u4e0b\u5b57\u6bb5<\/p>\n<p>text \u6a21\u578b\u751f\u6210\u7684\u6587\u672c<\/p>\n<p>finish_reason \u6a21\u578b\u505c\u6b62\u539f\u56e0<\/p>\n<p>\u8fd9\u6837\uff0c\u6211\u4eecprint\u83b7\u53d6\u5230\u4e86\u8f93\u51fa\u7ed3\u679c<\/p>\n<p>\u5bf9\u4e8echat\u6a21\u578b\u7684\u8c03\u7528<\/p>\n<p>\u793a\u4f8b\u5982\u4e0b<\/p>\n<table>\n<tr>\n<td>\n  import openai<\/p>\n<p>response = openai.ChatCompletion.create(model=&#8221;gpt-4&#8243;,<\/p>\n<p>messages=[{&#8220;role&#8221;: &#8220;system&#8221;, &#8220;content&#8221;: &#8220;You are a creative AI.&#8221;},<\/p>\n<p>{&#8220;role&#8221;: &#8220;user&#8221;, &#8220;content&#8221;: &#8220;\u8bf7\u7ed9\u6211\u7684\u82b1\u5e97\u8d77\u4e2a\u540d&#8221;}, ], temperature=0.8,<\/p>\n<p>max_tokens=60)<\/p>\n<p>print(response[&#8216;choices&#8217;][0][&#8216;message&#8217;][&#8216;content&#8217;])<\/td>\n<\/tr>\n<\/table>\n<p>\u4e00\u4e2a\u662f\u6d88\u606f\uff0c\u4e00\u4e2a\u662f\u89d2\u8272<\/p>\n<p>Role\u4e2d\u6211\u4eec\u8bbe\u5b9a\u4e86\u4e00\u4e2a\u89d2\u8272\uff0c\u53ef\u4ee5\u662fsystem\uff0cuser\u6216\u8005assistant<\/p>\n<p>\u7136\u540econtent \u6d88\u606f\u5185\u5bb9<\/p>\n<p>\u5bf9\u4e8esystem\uff0c\u4e3b\u8981\u7528\u4e8e\u8bbe\u5b9a\u5bf9\u8bdd\u7684\u80cc\u666f\u548c\u4e0a\u4e0b\u6587\uff0c\u5e2e\u52a9ai\u7406\u89e3\u5176\u5728\u5bf9\u8bdd\u4e2d\u7684\u5bf9\u8272\u548c\u4efb\u52a1\uff0c\u4f8b\u5982\uff0c\u53ef\u4ee5\u544a\u8bc9ai\uff0c\u4ed6\u662f\u5728\u626e\u6f14\u4e00\u4e2a\u533b\u751f\uff0c\u5f8b\u5e08\u7b49<\/p>\n<p>\u5bf9\u4e8euser,\u5219\u662f\u5b9e\u9645\u8bf7\u6c42ai\u60f3\u8981\u5176\u5e2e\u5fd9\u5b8c\u6210\u7684\u4e8b\u60c5<\/p>\n<p>\u5bf9\u4e8eassistant\uff0c\u5219\u662f\u6a21\u578b\u7684\u56de\u590d\uff0c\u53ef\u4ee5\u5728\u5176\u4e2d\u63d0\u4f9b\u7ed9ai\u4e4b\u524d\u5176\u56de\u590d\u7684\u5185\u5bb9\uff0c\u65b9\u4fbf\u5176\u8fdb\u884c\u8bb0\u5fc6\u548c\u6a21\u578b\u884c\u4e3a\u8bbe\u5b9a\u3002<\/p>\n<p>{&#8216;id&#8217;: &#8216;chatcmpl-2nZI6v1cW9E3Jg4w2Xtoql0M3XHfH&#8217;, &#8216;object&#8217;: &#8216;chat.completion&#8217;, &#8216;created&#8217;: 1677649420, &#8216;model&#8217;: &#8216;gpt-4&#8217;,<\/p>\n<p>&#8216;usage&#8217;: {&#8216;prompt_tokens&#8217;: 56, &#8216;completion_tokens&#8217;: 31, &#8216;total_tokens&#8217;: 87}, &#8216;choices&#8217;: [<\/p>\n<p>{&#8216;message&#8217;: {&#8216;role&#8217;: &#8216;assistant&#8217;, &#8216;content&#8217;: &#8216;\u4f60\u7684\u82b1\u5e97\u53ef\u4ee5\u53eb\u505a&#8221;\u82b1\u9999\u56db\u6ea2&#8221;\u3002&#8217;}, &#8216;finish_reason&#8217;: &#8216;stop&#8217;, &#8216;index&#8217;: 0}]}<\/p>\n<p>\u5176\u8fd4\u56de\u7684\u5185\u5bb9\u4e2dchoice\u8fd4\u56de\u4e86\u8bf7\u6c42\u65f6\u5e26\u7684role<\/p>\n<p>\u5bf9\u4e8e\u8fd9\u4e24\u79cd\u6a21\u578b\uff0c\u53ef\u4ee5\u4ece\u4e0a\u9762\u63d0\u4f9b\u7684\u4fe1\u606f\u770b\u51fa\u6765\u5404\u6709\u4f18\u7f3a\u70b9\u3002<\/p>\n<p>\u76f8\u8f83\u4e8eText\u6a21\u578b\uff0cChat\u6a21\u578b\u7684\u8bbe\u8ba1\u66f4\u9002\u5408\u5904\u7406\u5bf9\u8bdd\u6216\u8005\u591a\u8f6e\u6b21\u4ea4\u4e92\u7684\u60c5\u51b5\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u6d88\u606f\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u8fd9\u4e2a\u6d88\u606f\u5217\u8868\u53ef\u4ee5\u5305\u542bsystem\u3001user\u548cassistant\u7684\u5386\u53f2\u4fe1\u606f\uff0c\u4ece\u800c\u5728\u5904\u7406\u4ea4\u4e92\u5f0f\u5bf9\u8bdd\u65f6\u63d0\u4f9b\u66f4\u591a\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/p>\n<p>\u5f53\u7136\u5bf9\u4e8e\u7b80\u5355\u7684\u5355\u8f6e\u6587\u672c\u751f\u6210\u4efb\u52a1\uff0c\u4f7f\u7528Text\u6a21\u578b\u66f4\u52a0\u7b80\u5355\u53ef\u80fd<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u770b\u4e0b\u5982\u4f55\u4f7f\u7528LangChain\u6765\u8bbf\u95eeOpen AI<\/p>\n<p>\u9996\u5148\u662fText\u6a21\u578b<\/p>\n<table>\n<tr>\n<td>\n  import os<\/p>\n<p>os.environ[&#8220;OPENAI_API_KEY&#8221;] = &#8216;\u4f60\u7684Open API   Key&#8217;<\/p>\n<p>from langchain.llms import OpenAI<\/p>\n<p>llm = OpenAI(model=&#8221;text-davinci-003&#8243;, temperature=0.8,   max_tokens=60, )<\/p>\n<p>response = llm.predict(&#8220;\u8bf7\u7ed9\u6211\u7684\u82b1\u5e97\u8d77\u4e2a\u540d&#8221;)<\/p>\n<p>print(response)<\/td>\n<\/tr>\n<\/table>\n<p>\u8fd9\u662f\u5bf9Open AI\u7684\u7b80\u5355\u8c03\u7528<\/p>\n<p>\u5bfc\u5165LangChain\u7684OpenAI\u7c7b\uff0c\u521b\u5efa\u4e00\u4e2aLLM \u5927\u8bed\u8a00\u6a21\u578b\u5bf9\u8c61\uff0c\u7136\u540e\u6307\u5b9a\u5bf9\u8c61\u548c\u4e00\u4e9b\u751f\u6210\u53c2\u6570\uff0c\u4e4b\u540e\u8c03\u7528predict\u51fd\u6570\u5373\u53ef<\/p>\n<p>\u5bf9\u4e8eChat\u6a21\u578b<\/p>\n<table>\n<tr>\n<td>\n  import os<\/p>\n<p>os.environ[&#8220;OPENAI_API_KEY&#8221;] = &#8216;\u4f60\u7684Open API   Key&#8217;<\/p>\n<p>from langchain.chat_models import<\/p>\n<p>ChatOpenAIchat = ChatOpenAI(model=&#8221;gpt-4&#8243;, temperature=0.8,   max_tokens=60)<\/p>\n<p>from langchain.schema import (HumanMessage, SystemMessage)<\/p>\n<p>messages = [SystemMessage(content=&#8221;\u4f60\u662f\u4e00\u4e2a\u5f88\u68d2\u7684\u667a\u80fd\u52a9\u624b&#8221;),   HumanMessage(content=&#8221;\u8bf7\u7ed9\u6211\u7684\u82b1\u5e97\u8d77\u4e2a\u540d&#8221;)]<\/p>\n<p>response = chat(messages)<\/td>\n<\/tr>\n<\/table>\n<p>\u6bd4\u5982Text\u6a21\u578b\uff0c\u591a\u4f20\u5165\u4e86SystemMessage\u548cHumanMessage\u7c7b<\/p>\n<p>\u8fdb\u884c\u4e86\u4f20\u5165\uff0c\u5e76\u8c03\u7528chat\u83b7\u53d6\u5230\u4e86response\u53d8\u91cf\u4e2d<\/p>\n<p>Response\u5305\u542b\u7684\u5185\u5bb9\u5219\u662f<\/p>\n<p>content=&#8217;\u5f53\u7136\u53ef\u4ee5\uff0c\u53eb\u505a&#8221;\u82b1\u8bed\u79d8\u5883&#8221;\u600e\u4e48\u6837\uff1f&#8217; additional_kwargs={} example=False<\/p>\n<p>\u4e0a\u9762\u53ef\u4ee5\u770b\u51fa\uff0c\u5982\u679c\u5c1d\u8bd5\u8c03\u7528LangChain\u6846\u67b6\u7684\u51fd\u6570\uff0c\u5176\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u7b80\u5355\u3002<\/p>\n<p>\u9664\u4e86\u8fd9\u4e9b\u5927\u8bed\u8a00\u6a21\u578b\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u5728\u5f00\u6e90\u793e\u533aHugginFace\u7f51\u7ad9\u4e0a\u8bbf\u95ee\u66f4\u591a\u7684\u5f00\u6e90\u6a21\u578b<\/p>\n<p><a href=\"https:\/\/huggingface.co\/\">https:\/\/huggingface.co\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>LangChain\uff0c\u4e00\u4e2a\u7528\u4e8e\u5927 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5045"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5045"}],"version-history":[{"count":1,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5045\/revisions"}],"predecessor-version":[{"id":5050,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/5045\/revisions\/5050"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5045"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}