{"id":16805,"date":"2024-12-31T13:23:43","date_gmt":"2024-12-31T05:23:43","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=16805"},"modified":"2024-12-31T13:23:43","modified_gmt":"2024-12-31T05:23:43","slug":"openai-webrtc-python","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/en\/openai-webrtc-python\/","title":{"rendered":"OpenAI WebRTC Python\uff1a\u4e0eOpenAI\u5b9e\u65f6API\u8fdb\u884c\u8bed\u97f3\u4ea4\u4e92\u7684Python\u5e93"},"content":{"rendered":"<p>OpenAI Realtime WebRTC Python\u662f\u4e00\u4e2a\u4e13\u4e1a\u7684Python\u5e93\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e0eOpenAI\u5b9e\u65f6API\u8fdb\u884c\u8bed\u97f3\u4ea4\u4e92\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848\u3002\u8be5\u9879\u76ee\u57fa\u4e8eWebRTC\u6280\u672f\uff0c\u5b9e\u73b0\u4e86\u4f4e\u5ef6\u8fdf\u7684\u5b9e\u65f6\u97f3\u9891\u4f20\u8f93\u529f\u80fd\u3002\u5b83\u4e0d\u4ec5\u652f\u6301\u81ea\u52a8\u7684\u97f3\u9891\u8bbe\u5907\u7ba1\u7406\u548c\u91c7\u6837\u7387\u8f6c\u6362\uff0c\u8fd8\u63d0\u4f9b\u4e86\u5b8c\u5584\u7684\u97f3\u9891\u7f13\u51b2\u7ba1\u7406\u673a\u5236\u3002\u9879\u76ee\u91c7\u7528MIT\u8bb8\u53ef\u8bc1\u5f00\u6e90\uff0c\u652f\u6301Windows\u3001macOS\u548cLinux\u7b49\u591a\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u5e73\u53f0\u3002\u901a\u8fc7\u8be5\u5e93\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5b9e\u65f6\u8bed\u97f3\u8bc6\u522b\u3001\u97f3\u9891\u6d41\u5904\u7406\u7b49\u9ad8\u7ea7\u529f\u80fd\uff0c\u7279\u522b\u9002\u5408\u6784\u5efa\u9700\u8981\u5b9e\u65f6\u8bed\u97f3\u4ea4\u4e92\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u529f\u80fd\u5217\u8868<\/h2>\n<ul>\n<li>\u57fa\u4e8eWebRTC\u7684\u4f4e\u5ef6\u8fdf\u5b9e\u65f6\u97f3\u9891\u901a\u4fe1<\/li>\n<li>\u652f\u6301OpenAI\u6700\u65b0\u7684Realtime API\u63a5\u53e3<\/li>\n<li>\u667a\u80fd\u97f3\u9891\u8bbe\u5907\u81ea\u52a8\u7ba1\u7406\u4e0e\u914d\u7f6e<\/li>\n<li>\u81ea\u9002\u5e94\u97f3\u9891\u91c7\u6837\u7387\u8f6c\u6362\u529f\u80fd<\/li>\n<li>\u4e13\u4e1a\u7684\u97f3\u9891\u7f13\u51b2\u7ba1\u7406\u7cfb\u7edf<\/li>\n<li>\u652f\u6301\u97f3\u9891\u6d41\u7684\u6682\u505c\u4e0e\u6062\u590d\u63a7\u5236<\/li>\n<li>\u5f02\u6b65\u97f3\u9891\u5904\u7406\u548c\u4e8b\u4ef6\u56de\u8c03\u673a\u5236<\/li>\n<li>\u5185\u7f6e\u97f3\u9891\u8f6c\u5f55\u6587\u672c\u529f\u80fd<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u4f7f\u7528\u5e2e\u52a9<\/h2>\n<h3>\u73af\u5883\u51c6\u5907<\/h3>\n<ol>\n<li><strong>\u7cfb\u7edf\u8981\u6c42<\/strong>\n<ul>\n<li>Python 3.7\u6216\u66f4\u9ad8\u7248\u672c<\/li>\n<li>\u652f\u6301Windows\u3001macOS\u3001Linux\u64cd\u4f5c\u7cfb\u7edf<\/li>\n<li>\u786e\u4fdd\u7cfb\u7edf\u6709\u53ef\u7528\u7684\u97f3\u9891\u8bbe\u5907<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5b89\u88c5\u8fc7\u7a0b<\/strong>\n<pre><code># \u514b\u9686\u9879\u76ee\u4ee3\u7801\r\ngit clone https:\/\/github.com\/realtime-ai\/openai-realtime-webrtc-python.git\r\ncd openai-realtime-webrtc-python\r\n# \u521b\u5efa\u5e76\u6fc0\u6d3b\u865a\u62df\u73af\u5883\r\npython -m venv venv\r\nsource venv\/bin\/activate  # Linux\/macOS\u7cfb\u7edf\r\n# \u6216\u5728Windows\u7cfb\u7edf\u4f7f\u7528\uff1a\r\n# .\\venv\\Scripts\\activate\r\n# \u5b89\u88c5\u4f9d\u8d56\u5305\r\npip install -r requirements.txt\r\n# \u5f00\u53d1\u6a21\u5f0f\u5b89\u88c5\r\npip install -e .\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u914d\u7f6e\u8bbe\u7f6e<\/h3>\n<ol>\n<li><strong>\u73af\u5883\u53d8\u91cf\u914d\u7f6e<\/strong>\n<ul>\n<li>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u521b\u5efa<code>.env<\/code>\u6587\u4ef6<\/li>\n<li>\u6dfb\u52a0OpenAI API\u5bc6\u94a5\uff1a<\/li>\n<\/ul>\n<pre><code>OPENAI_API_KEY=your-api-key-here\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u57fa\u7840\u4f7f\u7528\u6d41\u7a0b<\/h3>\n<ol>\n<li><strong>\u521b\u5efa\u5ba2\u6237\u7aef\u5b9e\u4f8b<\/strong>\n<pre><code>import asyncio\r\nfrom openai_realtime_webrtc import OpenAIWebRTCClient\r\nasync def main():\r\nclient = OpenAIWebRTCClient(\r\napi_key=\"your-api-key\",\r\nmodel=\"gpt-4o-realtime-preview-2024-12-17\"\r\n)\r\n<\/code><\/pre>\n<\/li>\n<li><strong>\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570<\/strong>\n<pre><code>def on_transcription(text: str):\r\nprint(f\"\u8f6c\u5f55\u6587\u672c: {text}\")\r\nclient.on_transcription = on_transcription\r\n<\/code><\/pre>\n<\/li>\n<li><strong>\u542f\u52a8\u97f3\u9891\u6d41<\/strong>\n<pre><code>try:\r\n# \u5f00\u59cb\u97f3\u9891\u6d41\u4f20\u8f93\r\nawait client.start_streaming()\r\n# \u4fdd\u6301\u8fde\u63a5\u8fd0\u884c\r\nwhile True:\r\nawait asyncio.sleep(1)\r\nexcept KeyboardInterrupt:\r\n# \u7ec8\u6b62\u97f3\u9891\u6d41\r\nawait client.stop_streaming()\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>\u9ad8\u7ea7\u529f\u80fd\u4f7f\u7528<\/h3>\n<ol>\n<li><strong>\u97f3\u9891\u8bbe\u5907\u7ba1\u7406<\/strong>\n<ul>\n<li>\u7cfb\u7edf\u4f1a\u81ea\u52a8\u68c0\u6d4b\u548c\u7ba1\u7406\u53ef\u7528\u7684\u97f3\u9891\u8f93\u5165\u8bbe\u5907<\/li>\n<li>\u652f\u6301\u52a8\u6001\u5207\u6362\u97f3\u9891\u8bbe\u5907<\/li>\n<li>\u81ea\u52a8\u5904\u7406\u91c7\u6837\u7387\u8f6c\u6362<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u97f3\u9891\u6d41\u63a7\u5236<\/strong>\n<ul>\n<li>\u652f\u6301\u968f\u65f6\u6682\u505c\/\u6062\u590d\u97f3\u9891\u6d41<\/li>\n<li>\u63d0\u4f9b\u97f3\u9891\u7f13\u51b2\u533a\u7ba1\u7406<\/li>\n<li>\u81ea\u52a8\u5904\u7406\u7f51\u7edc\u5ef6\u8fdf\u548c\u6296\u52a8<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u9519\u8bef\u5904\u7406\u548c\u76d1\u63a7<\/strong>\n<ul>\n<li>\u5185\u7f6e\u9519\u8bef\u68c0\u6d4b\u548c\u5f02\u5e38\u5904\u7406\u673a\u5236<\/li>\n<li>\u652f\u6301\u97f3\u9891\u8d28\u91cf\u76d1\u63a7<\/li>\n<li>\u63d0\u4f9b\u8be6\u7ec6\u7684\u8c03\u8bd5\u4fe1\u606f<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<li>\u786e\u4fdd\u7f51\u7edc\u8fde\u63a5\u7a33\u5b9a<\/li>\n<li>\u5b9a\u671f\u68c0\u67e5API\u5bc6\u94a5\u7684\u6709\u6548\u6027<\/li>\n<li>\u6ce8\u610f\u76d1\u63a7\u97f3\u9891\u8bbe\u5907\u7684\u72b6\u6001<\/li>\n<li>\u5408\u7406\u63a7\u5236\u97f3\u9891\u6d41\u7684\u542f\u505c\u65f6\u673a<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>OpenAI Realtime WebRTC Python\u662f\u4e00\u4e2a\u4e13\u4e1a\u7684Python\u5e93\uff0c\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e0eOpenAI\u5b9e\u65f6API\u8fdb\u884c\u8bed\u97f3\u4ea4\u4e92\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848\u3002\u8be5\u9879\u76ee\u57fa\u4e8eWebRTC\u6280\u672f\uff0c\u5b9e\u73b0\u4e86\u4f4e\u5ef6\u8fdf\u7684\u5b9e\u65f6\u97f3\u9891\u4f20\u8f93\u529f\u80fd\u3002\u5b83\u4e0d\u4ec5\u652f\u6301\u81ea\u52a8\u7684\u97f3\u9891\u8bbe\u5907\u7ba1\u7406&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[230],"class_list":["post-16805","post","type-post","status-publish","format-standard","hentry","category-tool","tag-aikaiyuanxiangmu"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/16805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/comments?post=16805"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/16805\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/media?parent=16805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/categories?post=16805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/tags?post=16805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}