-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentic_base.py
More file actions
61 lines (52 loc) · 1.79 KB
/
Copy pathagentic_base.py
File metadata and controls
61 lines (52 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import re
from abc import ABC
import torch
from openai import OpenAI
from pydantic import ValidationError
from transformers import logging
from agentic_utils import *
logging.set_verbosity_debug()
print(torch.cuda.get_arch_list())
class BaseClientAgent(ABC):
def __init__(self, name, base_url="", model=""):
self.name = name
self.client = OpenAI(
base_url=base_url,
api_key="EMPTY"
)
self.model = model
def ask(self, prompt):
print('got original prompt:', prompt, '\n\n')
completion = self.client.chat.completions.create(
extra_body={},
model=self.model,
temperature=0,
messages=[
{
"role": "user",
"content": prompt
}
]
)
print('answer generated:', completion.choices[0].message.content)
return completion.choices[0].message.content
class CustomJSONValidator:
@staticmethod
def validate(response):
# Match the "### FINAL JSON ###" section with optional code block
match = re.search(r'''
### FINAL JSON ###
:? # Optional colon
\s* # Optional whitespace
(?:```json\s*)? # Optional code block start
(\{.*?\}) # Capture JSON object
(?:\s*```)? # Optional code block end
''', response, re.DOTALL | re.VERBOSE)
return match
@staticmethod
def pydantic_validate(response, typing):
if isinstance(response, str) and isinstance(typing, BaseModel):
try:
return typing.model_validate_json(response)
except ValidationError:
return None