{"id":20746,"date":"2025-02-10T19:30:11","date_gmt":"2025-02-10T11:30:11","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=20746"},"modified":"2025-08-25T00:26:32","modified_gmt":"2025-08-24T16:26:32","slug":"instructor","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/pt\/instructor\/","title":{"rendered":"Instructor\uff1a\u7b80\u5316\u5927\u8bed\u8a00\u6a21\u578b\u7ed3\u6784\u5316\u8f93\u51fa\u5de5\u4f5c\u6d41\u7684Python\u5e93"},"content":{"rendered":"<p>Instructor \u662f\u4e00\u4e2a\u6d41\u884c\u7684 Python \u5e93\uff0c\u4e13\u4e3a\u5904\u7406\u5927\u8bed\u8a00\u6a21\u578b\uff08LLMs\uff09\u7684\u7ed3\u6784\u5316\u8f93\u51fa\u800c\u8bbe\u8ba1\u3002\u5b83\u57fa\u4e8e Pydantic \u6784\u5efa\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u3001\u900f\u660e\u4e14\u7528\u6237\u53cb\u597d\u7684 API\uff0c\u7528\u4e8e\u7ba1\u7406\u6570\u636e\u9a8c\u8bc1\u3001\u91cd\u8bd5\u548c\u6d41\u5f0f\u54cd\u5e94\u3002Instructor \u6bcf\u6708\u4e0b\u8f7d\u91cf\u8d85\u8fc7\u4e00\u767e\u4e07\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd LLM \u5de5\u4f5c\u6d41\u4e2d\u3002\u8be5\u5e93\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u5305\u62ec Python\u3001TypeScript\u3001Ruby\u3001Go \u548c Elixir\uff0c\u80fd\u591f\u4e0e\u591a\u79cd LLM \u63d0\u4f9b\u5546\u65e0\u7f1d\u96c6\u6210\uff0c\u8d85\u8d8a\u4e86 OpenAI \u7684\u652f\u6301\u8303\u56f4\u3002<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<a href=\"https:\/\/www.kdjingpai.com\/damoxingjiegouhuaai\/\">\u5927\u6a21\u578b\u7ed3\u6784\u5316\u6570\u636e\u8f93\u51fa\u65b9\u6cd5\uff1a\u7cbe\u9009 LLM JSON \u8d44\u6e90\u5217\u8868<\/a>\u3001<a href=\"https:\/\/www.kdjingpai.com\/outlines\/\">Outlines\uff1a\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u3001JSON\u6216Pydantic\u6a21\u578b\u751f\u6210\u7ed3\u6784\u5316\u6587\u672c\u8f93\u51fa<\/a>\u3001<a href=\"https:\/\/www.kdjingpai.com\/ai-functions\/\">AI Functions\uff1a\u5c06\u8f93\u5165\u5185\u5bb9\u8f6c\u6362\u4e3a\u7ed3\u6784\u5316\u8f93\u51fa\u7684\uff08API\uff09\u670d\u52a1<\/a>\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-20748\" title=\"Instructor\uff1a\u7b80\u5316\u5927\u8bed\u8a00\u6a21\u578b\u5de5\u4f5c\u6d41\u7684\u7ed3\u6784\u5316\u8f93\u51faPython\u5e93-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/0eca94f369ce22b.png\" alt=\"Instructor\uff1a\u7b80\u5316\u5927\u8bed\u8a00\u6a21\u578b\u5de5\u4f5c\u6d41\u7684\u7ed3\u6784\u5316\u8f93\u51faPython\u5e93-1\" width=\"1067\" height=\"677\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/0eca94f369ce22b.png 1067w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/0eca94f369ce22b-768x487.png 768w\" sizes=\"auto, (max-width: 1067px) 100vw, 1067px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>\u529f\u80fd\u5217\u8868<\/h2>\n<ul>\n<li><strong>\u54cd\u5e94\u6a21\u578b<\/strong>\uff1a\u4f7f\u7528 Pydantic \u6a21\u578b\u5b9a\u4e49 LLM \u8f93\u51fa\u7684\u7ed3\u6784\u3002<\/li>\n<li><strong>\u91cd\u8bd5\u7ba1\u7406<\/strong>\uff1a\u8f7b\u677e\u914d\u7f6e\u8bf7\u6c42\u7684\u91cd\u8bd5\u6b21\u6570\u3002<\/li>\n<li><strong>\u6570\u636e\u9a8c\u8bc1<\/strong>\uff1a\u786e\u4fdd LLM \u54cd\u5e94\u7b26\u5408\u9884\u671f\u3002<\/li>\n<li><strong>\u6d41\u5f0f\u652f\u6301<\/strong>\uff1a\u8f7b\u677e\u5904\u7406\u5217\u8868\u548c\u90e8\u5206\u54cd\u5e94\u3002<\/li>\n<li><strong>\u7075\u6d3b\u7684\u540e\u7aef<\/strong>\uff1a\u4e0e\u591a\u79cd LLM \u63d0\u4f9b\u5546\u65e0\u7f1d\u96c6\u6210\u3002<\/li>\n<li><strong>\u591a\u8bed\u8a00\u652f\u6301<\/strong>\uff1a\u652f\u6301 Python\u3001TypeScript\u3001Ruby\u3001Go \u548c Elixir \u7b49\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u4f7f\u7528\u5e2e\u52a9<\/h2>\n<h3>\u5b89\u88c5\u6d41\u7a0b<\/h3>\n<p>\u8981\u5b89\u88c5 Instructor\uff0c\u53ea\u9700\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>pip install -U instructor\r\n<\/code><\/pre>\n<h3>\u57fa\u672c\u4f7f\u7528<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Instructor \u4ece\u81ea\u7136\u8bed\u8a00\u4e2d\u63d0\u53d6\u7ed3\u6784\u5316\u6570\u636e\uff1a<\/p>\n<pre><code>import instructor\r\nfrom pydantic import BaseModel\r\nfrom openai import OpenAI\r\n# \u5b9a\u4e49\u6240\u9700\u7684\u8f93\u51fa\u7ed3\u6784\r\nclass UserInfo(BaseModel):\r\nname: str\r\nage: int\r\n# \u521d\u59cb\u5316 OpenAI \u5ba2\u6237\u7aef\u5e76\u4e0e Instructor \u96c6\u6210\r\nclient = instructor.from_openai(OpenAI())\r\n# \u4ece\u81ea\u7136\u8bed\u8a00\u4e2d\u63d0\u53d6\u7ed3\u6784\u5316\u6570\u636e\r\nuser_info = client.chat.completions.create(\r\nmodel=\"gpt-4o-mini\",\r\nresponse_model=UserInfo,\r\nmessages=[{\"role\": \"user\", \"content\": \"John Doe is 30 years old.\"}]\r\n)\r\nprint(user_info.name)  # \u8f93\u51fa: John Doe\r\nprint(user_info.age)   # \u8f93\u51fa: 30\r\n<\/code><\/pre>\n<h3>\u4f7f\u7528\u94a9\u5b50<\/h3>\n<p>Instructor \u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u94a9\u5b50\u7cfb\u7edf\uff0c\u5141\u8bb8\u60a8\u5728 LLM \u4ea4\u4e92\u8fc7\u7a0b\u7684\u5404\u4e2a\u9636\u6bb5\u8fdb\u884c\u62e6\u622a\u548c\u65e5\u5fd7\u8bb0\u5f55\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u94a9\u5b50\uff1a<\/p>\n<pre><code>import instructor\r\nfrom openai import OpenAI\r\nfrom pydantic import BaseModel\r\nclass UserInfo(BaseModel):\r\nname: str\r\nage: int\r\n# \u521d\u59cb\u5316 OpenAI \u5ba2\u6237\u7aef\u5e76\u4e0e Instructor \u96c6\u6210\r\nclient = instructor.from_openai(OpenAI())\r\n# \u4f7f\u7528\u94a9\u5b50\u8bb0\u5f55\u4ea4\u4e92\u8fc7\u7a0b\r\nclient.add_hook(\"before_request\", lambda request: print(f\"Request: {request}\"))\r\nclient.add_hook(\"after_response\", lambda response: print(f\"Response: {response}\"))\r\n# \u4ece\u81ea\u7136\u8bed\u8a00\u4e2d\u63d0\u53d6\u7ed3\u6784\u5316\u6570\u636e\r\nuser_info = client.chat.completions.create(\r\nmodel=\"gpt-4o-mini\",\r\nresponse_model=UserInfo,\r\nmessages=[{\"role\": \"user\", \"content\": \"Jane Doe is 25 years old.\"}]\r\n)\r\nprint(user_info.name)  # \u8f93\u51fa: Jane Doe\r\nprint(user_info.age)   # \u8f93\u51fa: 25\r\n<\/code><\/pre>\n<h3>\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<p>Instructor \u8fd8\u652f\u6301\u4e0e\u5176\u4ed6 LLM \u63d0\u4f9b\u5546\u96c6\u6210\uff0c\u5e76\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u914d\u7f6e\u9009\u9879\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u8bf7\u6c42\u7684\u91cd\u8bd5\u6b21\u6570\u3001\u6570\u636e\u9a8c\u8bc1\u89c4\u5219\u548c\u6d41\u5f0f\u54cd\u5e94\u5904\u7406\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Instructor \u662f\u4e00\u4e2a\u6d41\u884c\u7684 Python \u5e93\uff0c\u4e13\u4e3a\u5904\u7406\u5927\u8bed\u8a00\u6a21\u578b\uff08LLMs\uff09\u7684\u7ed3\u6784\u5316\u8f93\u51fa\u800c\u8bbe\u8ba1\u3002\u5b83\u57fa\u4e8e Pydantic \u6784\u5efa\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u3001\u900f\u660e\u4e14\u7528\u6237\u53cb\u597d\u7684 API\uff0c\u7528\u4e8e\u7ba1\u7406\u6570\u636e\u9a8c\u8bc1\u3001\u91cd\u8bd5\u548c\u6d41\u5f0f\u54cd\u5e94\u3002Instructor \u6bcf\u6708\u4e0b&#8230;<\/p>\n","protected":false},"author":1,"featured_media":32782,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,499],"tags":[230,252],"class_list":["post-20746","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tool","category-document-extraction","tag-aikaiyuanxiangmu","tag-markdown"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/posts\/20746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/comments?post=20746"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/posts\/20746\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/media\/32782"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/media?parent=20746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/categories?post=20746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/pt\/wp-json\/wp\/v2\/tags?post=20746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}