Skip to content

Commit 85891bf

Browse files
committed
AIにセクションごとに分けたドキュメントを与えるようにした
1 parent 06ee5e6 commit 85891bf

File tree

3 files changed

+53
-46
lines changed

3 files changed

+53
-46
lines changed

app/[docs_id]/chatForm.tsx

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useState, FormEvent } from "react";
44
import { askAI } from "@/app/actions/chatActions";
55

6-
export function ChatForm() {
6+
export function ChatForm({ documentContent }: { documentContent: string }) {
77
const [inputValue, setInputValue] = useState("");
88
const [response, setResponse] = useState("");
99
const [isLoading, setIsLoading] = useState(false);
@@ -16,83 +16,84 @@ export function ChatForm() {
1616

1717
const formData = new FormData();
1818
formData.append("message", inputValue);
19+
formData.append("documentContent", documentContent);
1920

2021
const result = await askAI({ response: "", error: null }, formData);
2122

2223
if (result.error) {
2324
setResponse(`エラー: ${result.error}`);
24-
} else {
25+
} else {
2526
setResponse(result.response);
2627
}
27-
28+
2829
setIsLoading(false);
2930
};
3031
return (
3132
<>
3233
{isFormVisible && (
3334
<form className="border border-2 border-primary shadow-xl p-6 rounded-lg bg-base-100" style={{width:"100%", textAlign:"center", boxShadow:"-moz-initial"}} onSubmit={handleSubmit}>
34-
<h2 className="text-xl font-bold mb-4 text-left relative -top-2 font-mono h-2">
35-
AIへ質問
36-
</h2>
35+
<h2 className="text-xl font-bold mb-4 text-left relative -top-2 font-mono h-2">
36+
AIへ質問
37+
</h2>
3738
<div className="input-area" style={{height:"80px"}}>
38-
<textarea
39-
className="textarea textarea-white textarea-md"
40-
placeholder="質問を入力してください"
39+
<textarea
40+
className="textarea textarea-white textarea-md"
41+
placeholder="質問を入力してください"
4142
style={{width: "100%", height: "110px", resize: "none"}}
42-
value={inputValue}
43-
onChange={(e) => setInputValue(e.target.value)}
44-
disabled={isLoading}
45-
></textarea>
46-
</div>
47-
<br />
43+
value={inputValue}
44+
onChange={(e) => setInputValue(e.target.value)}
45+
disabled={isLoading}
46+
></textarea>
47+
</div>
48+
<br />
4849
<div className="controls" style={{position:"relative", top:"22px", display:"flex", alignItems:"center", justifyContent:"space-between"}}>
49-
<div className="left-icons">
50-
<button
51-
className="btn btn-soft btn-secondary rounded-full"
52-
onClick={() => setIsFormVisible(false)}
53-
>
50+
<div className="left-icons">
51+
<button
52+
className="btn btn-soft btn-secondary rounded-full"
53+
onClick={() => setIsFormVisible(false)}
54+
>
5455

55-
閉じる
56-
</button>
57-
</div>
58-
<div className="right-controls">
59-
<button
60-
type="submit"
61-
className="btn btn-soft btn-circle btn-primary rounded-full"
62-
title="送信"
56+
閉じる
57+
</button>
58+
</div>
59+
<div className="right-controls">
60+
<button
61+
type="submit"
62+
className="btn btn-soft btn-circle btn-primary rounded-full"
63+
title="送信"
6364
style={{marginTop:"10px"}}
64-
disabled={isLoading}
65-
>
66-
<span className="icon"></span>
67-
</button>
65+
disabled={isLoading}
66+
>
67+
<span className="icon"></span>
68+
</button>
69+
</div>
6870
</div>
69-
</div>
70-
</form>
71+
</form>
7172
)}
7273
{!isFormVisible && (
73-
<button
74-
className="btn btn-soft btn-secondary rounded-full"
75-
onClick={() => setIsFormVisible(true)}
76-
>
77-
チャットを開く
78-
</button>
74+
<button
75+
className="btn btn-soft btn-secondary rounded-full"
76+
onClick={() => setIsFormVisible(true)}
77+
>
78+
チャットを開く
79+
</button>
7980
)}
8081

8182
{response && (
8283
<article>
8384
<h3 className="text-lg font-semibold mb-2">AIの回答</h3>
8485
<div className="chat chat-start">
8586
<div className="chat-bubble chat-bubble-primary">
86-
<div className="response-container">{response}</div>
87+
<div className="response-container">{response}</div>
8788
</div>
8889
</div>
8990
</article>
9091
)}
9192

9293
{isLoading && (
93-
<div className="mt-2 text-l text-gray-500 animate-pulse">
94-
AIが考え中です…
95-
</div>
94+
<div className="mt-2 text-l text-gray-500 animate-pulse">
95+
AIが考え中です…
96+
</div>
9697
)}
9798

9899
</>

app/[docs_id]/section.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function Section({ section }: { section: MarkdownSection }) {
1111
<div>
1212
<Heading level={section.level}>{section.title}</Heading>
1313
<StyledMarkdown content={section.content} />
14-
<ChatForm />
14+
<ChatForm documentContent={section.content} />
1515
</div>
1616
);
1717
}

app/actions/chatActions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ export async function askAI(
1414
formData: FormData
1515
): Promise<FormState> {
1616
const message = formData.get('message');
17+
const documentContent = formData.get('documentContent');
1718

1819
if (!message || typeof message !== 'string' || message.trim() === '') {
1920
return { response: '', error: 'メッセージを入力してください。' };
2021
}
2122

23+
if (!documentContent || typeof documentContent !== 'string') {
24+
return { response: '', error: 'コンテキストとなるドキュメントがありません。' };
25+
}
26+
2227
try {
2328
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
24-
const result = await model.generateContent(message);
29+
const fullMessage = documentContent + "\n\n" + message;
30+
const result = await model.generateContent(fullMessage);
2531
const response = result.response;
2632
const text = response.text();
2733
return { response: text, error: null };

0 commit comments

Comments
 (0)