Skip to content

Commit 8237a53

Browse files
committed
[feat] Draft Copilot Extension LT
1 parent 0d64f0d commit 8237a53

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
======================================================================
2+
PythonでもGitHub Copilot Extensions作れるもん!
3+
======================================================================
4+
5+
PythonでもGitHub Copilot Extensions作れるもん!
6+
======================================================================
7+
8+
:Event: みんなのPython勉強会#110 LT
9+
:Presented: 2024/11/14 nikkie
10+
11+
お前、誰よ
12+
======================================================================
13+
14+
* nikkie @ Copilotを馬車馬のように働かせ隊
15+
* 機械学習エンジニア・自然言語処理(`We're hiring! <https://hrmos.co/pages/uzabase/jobs/1829077236709650481>`__)
16+
* `みんなのPython勉強会 <https://startpython.connpass.com/>`__ スタッフ・4代目LT王子
17+
18+
.. image:: ../_static/uzabase-white-logo.png
19+
20+
GitHub Copilot Extensionsって、何よ
21+
======================================================================
22+
23+
聞いたことありますか?🙋‍♂️
24+
25+
まず GitHub Copilot
26+
--------------------------------------------------
27+
28+
**AI ペア プログラマー**
29+
30+
`GitHub Copilot とは何ですか? <https://docs.github.com/ja/copilot/about-github-copilot/what-is-github-copilot>`__
31+
------------------------------------------------------------------------------------------------------------------------------------------------------
32+
33+
GitHub Copilot は AI コーディング アシスタントであり、コードをより速く楽に記述できるため、問題解決とコラボレーションにより多くのエネルギーを集中できます。
34+
35+
様々な機能を搭載
36+
--------------------------------------------------
37+
38+
:コード補完: 続くコードを提案してくれる。採用して少し直すだけ
39+
:チャット: ChatGPTがVS Codeに住んでいるイメージ。色々聞けちゃう
40+
41+
ほかにもまだまだ! なお進化中!
42+
43+
Copilot 棲息場所
44+
--------------------------------------------------
45+
46+
課金して有効にすると
47+
48+
* VS Code(`拡張 <https://marketplace.visualstudio.com/items?itemName=GitHub.copilot>`__ をインストール)
49+
* ブラウザで閲覧している GitHub の中 (github.com)
50+
51+
ご参考
52+
--------------------------------------------------
53+
54+
.. raw:: html
55+
56+
<iframe class="speakerdeck-iframe" style="border: 0px; background: rgba(0, 0, 0, 0.1) padding-box; margin: 0px; padding: 0px; border-radius: 6px; box-shadow: rgba(0, 0, 0, 0.2) 0px 5px 40px; width: 100%; height: auto; aspect-ratio: 560 / 315;" frameborder="0" src="https://speakerdeck.com/player/21bcfa0ac76a426e8b39ce92884a9f2a?slide=1" title="GitHub Copilot Tips and Tricks" allowfullscreen="true" data-ratio="1.7777777777777777"></iframe>
57+
58+
GitHub Copilot **Extensions**
59+
======================================================================
60+
61+
Extension=拡張
62+
63+
拡張、2つの方向性
64+
--------------------------------------------------
65+
66+
:VS Code チャット拡張機能: VS Code拡張としてCopilot Chatを拡張できる(@する先の *エージェント*)
67+
:GitHub Copilot Extensions: エージェントを作れる。このLTの本題
68+
69+
`Copilot 拡張機能の構築について <https://docs.github.com/ja/copilot/building-copilot-extensions/about-building-copilot-extensions>`__
70+
71+
GitHub Copilot Extensions
72+
--------------------------------------------------
73+
74+
* GitHub Copilotをカスタマイズして、 **独自のエージェント** (Copilot Chatの相手)を作れる
75+
* GitHubが用意する ``/chat/completions`` のエンドポイントを使える
76+
* 例えば特定のリポジトリでRAGをするエージェント(プライベートのコードベースで)
77+
78+
服部さんの発表で知りました
79+
--------------------------------------------------
80+
81+
.. raw:: html
82+
83+
<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/UKpbIv0_J4A?si=DgWvs__rNEf6PMAj&amp;start=1364" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
84+
85+
実装例
86+
--------------------------------------------------
87+
88+
* https://github.com/copilot-extensions
89+
* Node.js, TypeScript
90+
* Go
91+
92+
**Python** でもGitHub Copilot Extensions作れるもん!
93+
======================================================================
94+
95+
得意なPythonを使いたい...!
96+
--------------------------------------------------
97+
98+
* Hello worldにあたる `blackbeard-extension <https://github.com/copilot-extensions/blackbeard-extension>`__
99+
* 海賊黒ひげとしてChatに応じる(※ONE PIECEではない)
100+
* Node.jsによる実装
101+
102+
blackbeard-extension (Node.js) 抜粋
103+
--------------------------------------------------
104+
105+
.. code-block:: javascript
106+
107+
// https://github.com/copilot-extensions/blackbeard-extension/blob/11b5a9abaec14f57ee1c92350bf64553411deb02/index.js#L7-L48
108+
app.post("/", express.json(), async (req, res) => {
109+
const payload = req.body;
110+
const messages = payload.messages;
111+
messages.unshift({
112+
role: "system", content: "You are a helpful assistant that replies to user messages as if you were the Blackbeard Pirate.",
113+
});
114+
// Chatのユーザの入力にシステムプロンプトを加え、LLMに返答を生成させる
115+
const copilotLLMResponse = await fetch(
116+
"https://api.githubcopilot.com/chat/completions",
117+
// 省略
118+
)
119+
Readable.from(copilotLLMResponse.body).pipe(res);
120+
})
121+
122+
Pythonでもできました✌️(FastAPI❤️)
123+
--------------------------------------------------
124+
125+
.. code-block:: python
126+
127+
# https://github.com/ftnext/blackbeard-extension-python/blob/56ae295c54e2241645382a8a027a81316072b43b/app.py#L10-L40
128+
@app.post("/")
129+
async def stream(request: Request, x_github_token: str = Header(None)):
130+
payload = await request.json()
131+
messages = payload["messages"]
132+
messages.insert(
133+
0, {"role": "system", "content": "You are a helpful assistant that replies to user messages as if you were the Blackbeard Pirate."})
134+
135+
def pass_generator():
136+
with httpx.stream(
137+
"POST", "https://api.githubcopilot.com/chat/completions", headers=headers, json=data,
138+
) as response:
139+
# response.iter_lines() を yield
140+
141+
return StreamingResponse(pass_generator(), media_type="text/event-stream")
142+
143+
簡単に動かせます!
144+
--------------------------------------------------
145+
146+
Demo https://github.com/ftnext/blackbeard-extension-python
147+
148+
まとめ🌯 PythonでもGitHub Copilot Extensions作れるもん!
149+
======================================================================
150+
151+
* Copilot Chatのエージェントを作れるGitHub Copilot Extensions
152+
* **海賊黒ひげ拡張をPythonで実装** してみました
153+
* OpenAIのGPTsのようなものをプログラミングで作っている感覚でとってもワクワク
154+
155+
ご清聴ありがとうございました
156+
--------------------------------------------------
157+
158+
`blackbeard-extension-python <https://github.com/ftnext/blackbeard-extension-python>`__ お試しあれ!
159+
160+
☠️ゼハハハハ☠️

0 commit comments

Comments
 (0)