Spaces:
Runtime error
Runtime error
edit analysis
Browse files- analysis.py +27 -11
analysis.py
CHANGED
@@ -3,7 +3,8 @@ import json
|
|
3 |
from prompts import PRECEDENT_ANALYSIS_TEMPLATE
|
4 |
from enum import Enum
|
5 |
from anthropic import Anthropic
|
6 |
-
from llama_index.llms.openai import OpenAI
|
|
|
7 |
from llama_index.core.llms import ChatMessage
|
8 |
from config import embed_model, Settings, openai_api_key, anthropic_api_key
|
9 |
|
@@ -28,7 +29,7 @@ class LLMAnalyzer:
|
|
28 |
self.model_name = model_name
|
29 |
|
30 |
if provider == ModelProvider.OPENAI:
|
31 |
-
self.client = OpenAI(
|
32 |
elif provider == ModelProvider.ANTHROPIC:
|
33 |
# Додаємо API ключ при ініціалізації
|
34 |
self.client = Anthropic(api_key=anthropic_api_key)
|
@@ -42,12 +43,6 @@ class LLMAnalyzer:
|
|
42 |
return await self._analyze_with_anthropic(prompt, response_schema)
|
43 |
|
44 |
async def _analyze_with_openai(self, prompt: str, response_schema: dict) -> str:
|
45 |
-
messages = [
|
46 |
-
ChatMessage(role="system",
|
47 |
-
content="Ти - кваліфікований юрист-аналітик, експерт з правових позицій Верховного Суду."),
|
48 |
-
ChatMessage(role="user", content=prompt)
|
49 |
-
]
|
50 |
-
|
51 |
# Правильний формат для response_format
|
52 |
response_format = {
|
53 |
"type": "json_schema",
|
@@ -57,13 +52,34 @@ class LLMAnalyzer:
|
|
57 |
}
|
58 |
}
|
59 |
|
60 |
-
response = self.client.chat(
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
response_format=response_format,
|
63 |
temperature=0,
|
64 |
max_tokens=4096
|
65 |
)
|
66 |
-
|
|
|
67 |
|
68 |
async def _analyze_with_anthropic(self, prompt: str, response_schema: dict) -> str:
|
69 |
response = self.client.messages.create( # Прибрали await
|
|
|
3 |
from prompts import PRECEDENT_ANALYSIS_TEMPLATE
|
4 |
from enum import Enum
|
5 |
from anthropic import Anthropic
|
6 |
+
# from llama_index.llms.openai import OpenAI
|
7 |
+
from openai import OpenAI
|
8 |
from llama_index.core.llms import ChatMessage
|
9 |
from config import embed_model, Settings, openai_api_key, anthropic_api_key
|
10 |
|
|
|
29 |
self.model_name = model_name
|
30 |
|
31 |
if provider == ModelProvider.OPENAI:
|
32 |
+
self.client = OpenAI()
|
33 |
elif provider == ModelProvider.ANTHROPIC:
|
34 |
# Додаємо API ключ при ініціалізації
|
35 |
self.client = Anthropic(api_key=anthropic_api_key)
|
|
|
43 |
return await self._analyze_with_anthropic(prompt, response_schema)
|
44 |
|
45 |
async def _analyze_with_openai(self, prompt: str, response_schema: dict) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
# Правильний формат для response_format
|
47 |
response_format = {
|
48 |
"type": "json_schema",
|
|
|
52 |
}
|
53 |
}
|
54 |
|
55 |
+
response = self.client.chat.completions.create(
|
56 |
+
model=self.model_name,
|
57 |
+
messages=[
|
58 |
+
{
|
59 |
+
"role": "system",
|
60 |
+
"content": [
|
61 |
+
{
|
62 |
+
"type": "text",
|
63 |
+
"text": "Ти - кваліфікований юрист-аналітик, експерт з правових позицій Верховного Суду."
|
64 |
+
}
|
65 |
+
]
|
66 |
+
},
|
67 |
+
{
|
68 |
+
"role": "user",
|
69 |
+
"content": [
|
70 |
+
{
|
71 |
+
"type": "text",
|
72 |
+
"text": prompt
|
73 |
+
}
|
74 |
+
]
|
75 |
+
}
|
76 |
+
],
|
77 |
response_format=response_format,
|
78 |
temperature=0,
|
79 |
max_tokens=4096
|
80 |
)
|
81 |
+
|
82 |
+
return response.choices[0].message.content
|
83 |
|
84 |
async def _analyze_with_anthropic(self, prompt: str, response_schema: dict) -> str:
|
85 |
response = self.client.messages.create( # Прибрали await
|