@@ -17,9 +17,8 @@ type ChatResult =
1717 } ;
1818
1919type ChatParams = {
20+ langName : string ;
2021 userQuestion : string ;
21- docsId : string ;
22- documentContent : string ;
2322 sectionContent : DynamicMarkdownSection [ ] ;
2423 replOutputs : Record < string , ReplCommand [ ] > ;
2524 files : Record < string , string > ;
@@ -37,8 +36,8 @@ export async function askAI(params: ChatParams): Promise<ChatResult> {
3736 // }
3837
3938 const {
39+ langName,
4040 userQuestion,
41- documentContent,
4241 sectionContent,
4342 replOutputs,
4443 files,
@@ -47,8 +46,9 @@ export async function askAI(params: ChatParams): Promise<ChatResult> {
4746
4847 const prompt : string [ ] = [ ] ;
4948
49+ prompt . push ( `あなたは${ langName } 言語のチュートリアルの講師をしています。` ) ;
5050 prompt . push (
51- `以下のPythonチュートリアルのドキュメントの内容を正確に理解し 、ユーザーからの質問に対して、初心者にも分かりやすく、丁寧な解説を提供してください。`
51+ `以下の ${ langName } チュートリアルのドキュメントの内容を正確に理解し 、ユーザーからの質問に対して、初心者にも分かりやすく、丁寧な解説を提供してください。`
5252 ) ;
5353 prompt . push ( `` ) ;
5454 const sectionTitlesInView = sectionContent
@@ -64,8 +64,13 @@ export async function askAI(params: ChatParams): Promise<ChatResult> {
6464 prompt . push ( `` ) ;
6565 prompt . push ( `# ドキュメント` ) ;
6666 prompt . push ( `` ) ;
67- prompt . push ( documentContent ) ;
67+ for ( const section of sectionContent ) {
68+ prompt . push ( `[セクションid: ${ section . id } ]` ) ;
69+ prompt . push ( section . rawContent . trim ( ) ) ;
70+ prompt . push ( `` ) ;
71+ }
6872 prompt . push ( `` ) ;
73+ // TODO: 各セクションのドキュメントの直下にそのセクション内のターミナルの情報を加えるべきなのでは?
6974 if ( Object . keys ( replOutputs ) . length > 0 ) {
7075 prompt . push (
7176 `# ターミナルのログ(ユーザーが入力したコマンドとその実行結果)`
@@ -75,7 +80,7 @@ export async function askAI(params: ChatParams): Promise<ChatResult> {
7580 "以下はドキュメント内で実行例を示した各コードブロックの内容に加えてユーザーが追加で実行したコマンドです。"
7681 ) ;
7782 prompt . push (
78- "例えば ```python-repl:1 のコードブロックに対してユーザーが実行したログが ターミナル #1 です。"
83+ "例えば ```python-repl:foo のコードブロックに対してユーザーが実行したログが ターミナル #foo です。"
7984 ) ;
8085 prompt . push ( `` ) ;
8186 for ( const [ replId , replCommands ] of Object . entries ( replOutputs ) ) {
@@ -125,34 +130,40 @@ export async function askAI(params: ChatParams): Promise<ChatResult> {
125130 }
126131 }
127132
128- prompt . push ( "# ユーザーからの質問" ) ;
129- prompt . push ( userQuestion ) ;
130- prompt . push ( `` ) ;
131-
132133 prompt . push ( "# 指示" ) ;
134+ prompt . push ( "" ) ;
135+ prompt . push (
136+ `- 1行目に、ユーザーの質問ともっとも関連性の高いドキュメント内のセクションのidを回答してください。idのみを出力してください。`
137+ ) ;
138+ prompt . push (
139+ " - ユーザーの質問がドキュメントのどのセクションとも直接的に関連しない場合は空白でも良いです。"
140+ ) ;
141+ prompt . push ( "- 2行目は水平線 --- を出力してください。" )
142+ prompt . push (
143+ "- それ以降の行に、ドキュメントの内容に基づいて、ユーザーに伝える回答をMarkdown形式で記述してください。"
144+ ) ;
133145 prompt . push (
134- "- 回答はMarkdown形式で記述し、コードブロックを適切に使用してください 。"
146+ " - ユーザーが入力したターミナルのコマンドやファイルの内容、実行結果を参考にして回答してください 。"
135147 ) ;
136- prompt . push ( "- ドキュメントの内容に基づいて回答してください 。" ) ;
148+ prompt . push ( " - 必要であれば、具体的なコード例を提示してください 。" ) ;
137149 prompt . push (
138- "- ユーザーが入力したターミナルのコマンドやファイルの内容、実行結果を参考にして回答してください。"
150+ " - 回答内でコードブロックを使用する際は ```言語名 としてください。" +
151+ "ドキュメント内では ```言語名-repl や ```言語名:ファイル名 、 ```言語名-exec:ファイル名 などが登場しますが、ユーザーへの回答ではこれらの記法は使用しないでください。"
139152 ) ;
140- prompt . push ( "- ユーザーへの回答のみを出力してください。" ) ;
141- prompt . push ( "- 必要であれば、具体的なコード例を提示してください。" ) ;
153+ prompt . push ( " - 水平線(---)はシステムが区切りとして認識するので、ユーザーへの回答中に水平線を使用することはできません。" ) ;
142154 console . log ( prompt ) ;
143155
144156 try {
145- const result = await generateContent ( prompt . join ( "\n" ) ) ;
157+ const result = await generateContent ( userQuestion , prompt . join ( "\n" ) ) ;
146158 const text = result . text ;
147159 if ( ! text ) {
148160 throw new Error ( "AIからの応答が空でした" ) ;
149161 }
150- // TODO: どのセクションへの回答にするかをAIに決めさせる
151- const targetSectionId =
152- sectionContent . find ( ( s ) => s . inView ) ?. id || "" ;
153- const newChat = await addChat ( params . docsId , targetSectionId , [
162+ const targetSectionId = text . split ( / - { 3 , } / ) [ 0 ] . trim ( ) ;
163+ const responseMessage = text . split ( / - { 3 , } / ) [ 1 ] . trim ( ) ;
164+ const newChat = await addChat ( targetSectionId , [
154165 { role : "user" , content : userQuestion } ,
155- { role : "ai" , content : text } ,
166+ { role : "ai" , content : responseMessage } ,
156167 ] ) ;
157168 return {
158169 error : null ,
0 commit comments