Spaces:
Running
Running
Delete services
Browse files- services/__init__.py +0 -0
- services/llm_service.py +0 -124
services/__init__.py
DELETED
File without changes
|
services/llm_service.py
DELETED
@@ -1,124 +0,0 @@
|
|
1 |
-
from typing import Optional, List, Dict, Any
|
2 |
-
from openai import OpenAI
|
3 |
-
import anthropic
|
4 |
-
from dataclasses import dataclass
|
5 |
-
from config.llm_settings import LLMSettings
|
6 |
-
from core.file_scanner import FileInfo
|
7 |
-
|
8 |
-
@dataclass
|
9 |
-
class Message:
|
10 |
-
role: str
|
11 |
-
content: str
|
12 |
-
|
13 |
-
class LLMService:
|
14 |
-
MAX_TURNS = 5
|
15 |
-
|
16 |
-
def __init__(self):
|
17 |
-
"""LLMサービスの初期化"""
|
18 |
-
self.settings = LLMSettings()
|
19 |
-
self.current_model = self.settings.default_llm
|
20 |
-
|
21 |
-
# API クライアントの初期化
|
22 |
-
if self.settings.anthropic_api_key:
|
23 |
-
self.claude_client = anthropic.Anthropic(api_key=self.settings.anthropic_api_key)
|
24 |
-
if self.settings.openai_api_key:
|
25 |
-
self.openai_client = OpenAI(api_key=self.settings.openai_api_key)
|
26 |
-
|
27 |
-
self.conversation_history: List[Message] = []
|
28 |
-
|
29 |
-
def switch_model(self, model: str):
|
30 |
-
"""使用するモデルを切り替え"""
|
31 |
-
model_name = model.lower()
|
32 |
-
if model_name == "claude" and self.settings.anthropic_api_key:
|
33 |
-
self.current_model = "claude"
|
34 |
-
elif model_name == "openai" and self.settings.openai_api_key:
|
35 |
-
self.current_model = "openai"
|
36 |
-
else:
|
37 |
-
available = []
|
38 |
-
if self.settings.anthropic_api_key:
|
39 |
-
available.append("claude")
|
40 |
-
if self.settings.openai_api_key:
|
41 |
-
available.append("openai")
|
42 |
-
raise ValueError(f"利用可能なモデル: {', '.join(available)}")
|
43 |
-
|
44 |
-
def create_prompt(self, content: str, query: str) -> str:
|
45 |
-
"""プロンプトを生成"""
|
46 |
-
return f"""以下はGitHubリポジトリのコード解析結果です。このコードについて質問に答えてください。
|
47 |
-
|
48 |
-
コード解析結果:
|
49 |
-
{content}
|
50 |
-
|
51 |
-
質問: {query}
|
52 |
-
|
53 |
-
できるだけ具体的に、コードの内容を参照しながら回答してください。"""
|
54 |
-
|
55 |
-
def _add_to_history(self, role: str, content: str):
|
56 |
-
"""会話履歴に追加(最大5ターン)"""
|
57 |
-
self.conversation_history.append(Message(role=role, content=content))
|
58 |
-
# 最大ターン数を超えた場合、古い会話を削除
|
59 |
-
if len(self.conversation_history) > self.MAX_TURNS * 2: # 各ターンは質問と回答で2メッセージ
|
60 |
-
self.conversation_history = self.conversation_history[-self.MAX_TURNS * 2:]
|
61 |
-
|
62 |
-
def _format_messages_for_claude(self) -> List[Dict[str, str]]:
|
63 |
-
"""Claude用にメッセージをフォーマット"""
|
64 |
-
return [{"role": msg.role, "content": msg.content}
|
65 |
-
for msg in self.conversation_history]
|
66 |
-
|
67 |
-
def _format_messages_for_gpt(self) -> List[Dict[str, str]]:
|
68 |
-
"""GPT用にメッセージをフォーマット"""
|
69 |
-
return [
|
70 |
-
{"role": "system", "content": "あなたはコードアナリストとして、リポジトリの解析と質問への回答を行います。"},
|
71 |
-
*[{"role": msg.role, "content": msg.content}
|
72 |
-
for msg in self.conversation_history]
|
73 |
-
]
|
74 |
-
|
75 |
-
def get_conversation_history(self) -> List[Dict[str, str]]:
|
76 |
-
"""会話履歴を取得"""
|
77 |
-
return [{"role": msg.role, "content": msg.content}
|
78 |
-
for msg in self.conversation_history]
|
79 |
-
|
80 |
-
def clear_history(self):
|
81 |
-
"""会話履歴をクリア"""
|
82 |
-
self.conversation_history = []
|
83 |
-
|
84 |
-
def get_response(self, content: str, query: str) -> tuple[Optional[str], Optional[str]]:
|
85 |
-
"""LLMを使用して回答を生成"""
|
86 |
-
try:
|
87 |
-
prompt = self.create_prompt(content, query)
|
88 |
-
self._add_to_history("user", prompt)
|
89 |
-
|
90 |
-
print(f"Current model: {self.current_model}") # デバッグ用出力
|
91 |
-
|
92 |
-
if self.current_model.lower() == 'claude':
|
93 |
-
print("Using Claude API") # デバッグ用出力
|
94 |
-
response = self.claude_client.messages.create(
|
95 |
-
model="claude-3-5-sonnet-latest",
|
96 |
-
messages=self._format_messages_for_claude(),
|
97 |
-
max_tokens=1024
|
98 |
-
)
|
99 |
-
answer = response.content[0].text
|
100 |
-
|
101 |
-
else: # gpt
|
102 |
-
print("Using GPT API") # デバッグ用出力
|
103 |
-
response = self.openai_client.chat.completions.create(
|
104 |
-
model="gpt-4o",
|
105 |
-
messages=self._format_messages_for_gpt(),
|
106 |
-
max_tokens=1024
|
107 |
-
)
|
108 |
-
answer = response.choices[0].message.content
|
109 |
-
|
110 |
-
self._add_to_history("assistant", answer)
|
111 |
-
return answer, None
|
112 |
-
|
113 |
-
except Exception as e:
|
114 |
-
return None, f"エラーが発生しました: {str(e)}"
|
115 |
-
|
116 |
-
@staticmethod
|
117 |
-
def format_code_content(files: List[FileInfo]) -> str:
|
118 |
-
"""ファイル内容をプロンプト用にフォーマット"""
|
119 |
-
formatted_content = []
|
120 |
-
for file_info in files:
|
121 |
-
formatted_content.append(
|
122 |
-
f"#ファイルパス\n{file_info.path}\n------------\n{file_info.content}\n"
|
123 |
-
)
|
124 |
-
return "\n".join(formatted_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|