Spaces:
Sleeping
Sleeping
Delete services
Browse files- services/llm_service.py +0 -62
services/llm_service.py
DELETED
@@ -1,62 +0,0 @@
|
|
1 |
-
import anthropic
|
2 |
-
from dataclasses import dataclass
|
3 |
-
from typing import List, Optional, Dict
|
4 |
-
from core.file_scanner import FileInfo
|
5 |
-
|
6 |
-
@dataclass
|
7 |
-
class Message:
|
8 |
-
role: str
|
9 |
-
content: str
|
10 |
-
|
11 |
-
class LLMService:
|
12 |
-
MAX_TURNS = 5
|
13 |
-
|
14 |
-
def __init__(self, api_key: str):
|
15 |
-
self.client = anthropic.Anthropic(api_key=api_key)
|
16 |
-
self.conversation_history: List[Message] = []
|
17 |
-
|
18 |
-
def create_prompt(self, content: str, query: str) -> str:
|
19 |
-
return f"""以下はGitHubリポジトリのコード解析結果です。このコードについて質問に答えてください。
|
20 |
-
|
21 |
-
コード解析結果:
|
22 |
-
{content}
|
23 |
-
|
24 |
-
質問: {query}
|
25 |
-
|
26 |
-
できるだけ具体的に、コードの内容を参照しながら回答してください。"""
|
27 |
-
|
28 |
-
def _add_to_history(self, role: str, content: str):
|
29 |
-
self.conversation_history.append(Message(role=role, content=content))
|
30 |
-
if len(self.conversation_history) > self.MAX_TURNS * 2:
|
31 |
-
self.conversation_history = self.conversation_history[-self.MAX_TURNS * 2:]
|
32 |
-
|
33 |
-
def get_response(self, content: str, query: str) -> tuple[Optional[str], Optional[str]]:
|
34 |
-
try:
|
35 |
-
prompt = self.create_prompt(content, query)
|
36 |
-
self._add_to_history("user", prompt)
|
37 |
-
|
38 |
-
response = self.client.messages.create(
|
39 |
-
model="claude-3-5-sonnet-latest",
|
40 |
-
messages=[{"role": msg.role, "content": msg.content}
|
41 |
-
for msg in self.conversation_history],
|
42 |
-
max_tokens=1024
|
43 |
-
)
|
44 |
-
|
45 |
-
answer = response.content[0].text
|
46 |
-
self._add_to_history("assistant", answer)
|
47 |
-
return answer, None
|
48 |
-
|
49 |
-
except Exception as e:
|
50 |
-
return None, f"エラーが発生しました: {str(e)}"
|
51 |
-
|
52 |
-
def clear_history(self):
|
53 |
-
self.conversation_history = []
|
54 |
-
|
55 |
-
@staticmethod
|
56 |
-
def format_code_content(files: List[FileInfo]) -> str:
|
57 |
-
formatted_content = []
|
58 |
-
for file_info in files:
|
59 |
-
formatted_content.append(
|
60 |
-
f"#ファイルパス\n{file_info.path}\n------------\n{file_info.content}\n"
|
61 |
-
)
|
62 |
-
return "\n".join(formatted_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|