참고소스 수정본

This commit is contained in:
LASTA_DEV01\lasta
2026-05-12 19:40:31 +09:00
parent 0f34a451fc
commit 2e9204243d
8708 changed files with 3259488 additions and 869 deletions

View File

@@ -0,0 +1,12 @@
from .mock_async_validator_service import MockAsyncValidatorService
from .mock_custom_llm import MockAsyncOpenAILlm, MockOpenAILlm
from .mock_loop import MockLoop
from .mock_sequential_validator_service import MockSequentialValidatorService
__all__ = [
"MockAsyncValidatorService",
"MockSequentialValidatorService",
"MockLoop",
"MockOpenAILlm",
"MockAsyncOpenAILlm",
]

View File

@@ -0,0 +1,17 @@
import asyncio
class MockAsyncValidatorService:
initialized: bool
def __init__(self, *args, **kwargs):
self.initialized = True
async def async_validate(self, *args):
await asyncio.sleep(0.1)
# The return value doesn't really matter here.
# We just need something to identify which class and method was called.
return "MockAsyncValidatorService.async_validate", {"async": True}
def validate(self, *args):
return "MockAsyncValidatorService.validate", {"sync": True}

View File

@@ -0,0 +1,38 @@
from unittest.mock import Mock
from openai import APIError
class MockOpenAILlm:
def __init__(self, times_called=0, response="Hello world!"):
self.times_called = times_called
self.response = response
def fail_retryable(self, messages, *args, **kwargs) -> str:
if self.times_called == 0:
self.times_called = self.times_called + 1
raise APIError("ServiceUnavailableError", Mock(), body=None)
return self.response
def fail_non_retryable(self, messages, *args, **kwargs) -> str:
raise Exception("Non-Retryable Error!")
def succeed(self, messages, *args, **kwargs) -> str:
return self.response
class MockAsyncOpenAILlm:
def __init__(self, times_called=0, response="Hello world!"):
self.times_called = times_called
self.response = response
async def fail_retryable(self, messages, *args, **kwargs) -> str:
if self.times_called == 0:
self.times_called = self.times_called + 1
raise APIError("ServiceUnavailableError", Mock(), body=None)
return self.response
async def fail_non_retryable(self, messages, *args, **kwargs) -> str:
raise Exception("Non-Retryable Error!")
async def succeed(self, messages, *args, **kwargs) -> str:
return self.response

View File

@@ -0,0 +1,31 @@
from contextlib import AbstractContextManager
from types import TracebackType
from typing import Optional, Type
class MockFile(AbstractContextManager):
def __exit__(
self,
__exc_type: Optional[Type[BaseException]],
__exc_value: Optional[BaseException],
__traceback: Optional[TracebackType],
) -> Optional[bool]:
return super().__exit__(__exc_type, __exc_value, __traceback)
def readlines(self):
pass
def writelines(self, *args):
pass
def close(self):
pass
def read(self, *args):
pass
def write(self, *args):
pass
def seek(self, *args):
pass

View File

@@ -0,0 +1,47 @@
import os
def make_mock_model_and_tokenizer():
"""Returns a tuple of HF AutoModelForCausalLM and AutoTokenizer."""
import torch
torch.set_num_threads(1)
from transformers import AutoModelForCausalLM, AutoTokenizer
# Can regenerate the sample pipe with this:
# pipeline(
# "text-generation",
# "hf-internal-testing/tiny-random-gpt2",
# ).save_pretrained("...")
savedir = os.path.join(
os.path.abspath(os.path.normpath(os.path.dirname(__file__))), "tiny-random-gpt2"
)
model = AutoModelForCausalLM.from_pretrained(
savedir,
local_files_only=True,
)
tokenizer = AutoTokenizer.from_pretrained(
savedir,
local_files_only=True,
)
return model, tokenizer
def make_mock_pipeline():
from transformers import pipeline
model, tokenizer = make_mock_model_and_tokenizer()
pipe = pipeline(
task="text-generation",
model=model,
tokenizer=tokenizer,
trust_remote_code=False,
device_map="cpu", # Force CPU to avoid multithreaded fighting.
)
return pipe

View File

@@ -0,0 +1,14 @@
from typing import Any, Dict
from guardrails.validator_base import (
PassResult,
ValidationResult,
Validator,
register_validator,
)
@register_validator(name="mock-validator", data_type="string")
class MockValidator(Validator):
def validate(self, value: Any, metadata: Dict) -> ValidationResult:
return PassResult()

View File

@@ -0,0 +1,12 @@
class MockLoop:
def __init__(self, loop_is_running: bool):
self.loop_is_running = loop_is_running
def is_running(self):
return self.loop_is_running
def run_until_complete(self, future):
return future
def run_in_executor(self, executor, func, *args):
return func(*args)

View File

@@ -0,0 +1,8 @@
class MockSequentialValidatorService:
initialized: bool
def __init__(self, *args, **kwargs):
self.initialized = True
def validate(self, *args):
return "MockSequentialValidatorService.validate", {"sync": True}

View File

@@ -0,0 +1,18 @@
from contextlib import AbstractContextManager
from types import TracebackType
from typing import Optional, Type
from unittest.mock import MagicMock
class MockSpan(AbstractContextManager):
def __exit__(
self,
__exc_type: Optional[Type[BaseException]],
__exc_value: Optional[BaseException],
__traceback: Optional[TracebackType],
) -> Optional[bool]:
return super().__exit__(__exc_type, __exc_value, __traceback)
def __init__(self):
super().__init__()
self.set_attribute = MagicMock()

View File

@@ -0,0 +1,37 @@
from typing import Any, Callable, Dict, Union
from guardrails import Validator, register_validator
from guardrails_ai.types import (
FailResult,
PassResult,
ValidationResult,
)
def create_mock_validator(
name: str,
on_fail: Union[str, Callable] = None,
should_pass: bool = True,
return_value: Any = None,
):
def validate(self, value: Any, metadata: Dict[str, Any]) -> ValidationResult:
if self.should_pass:
return self.return_value if self.return_value is not None else PassResult()
else:
return FailResult(
error_message="Value is not valid.",
)
validator_type = type(
name,
(Validator,),
{
"validate": validate,
"name": name,
"on_fail": on_fail,
"should_pass": should_pass,
"return_value": return_value,
},
)
register_validator(name=name, data_type=["string"])(validator_type)
return validator_type

View File

@@ -0,0 +1,40 @@
{
"_name_or_path": "hf-internal-testing/tiny-random-gpt2",
"activation_function": "gelu_new",
"architectures": [
"GPT2LMHeadModel"
],
"attention_probs_dropout_prob": 0.1,
"attn_pdrop": 0.1,
"bos_token_id": 98,
"embd_pdrop": 0.1,
"eos_token_id": 98,
"gradient_checkpointing": false,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"initializer_range": 0.02,
"intermediate_size": 37,
"layer_norm_epsilon": 1e-05,
"model_type": "gpt2",
"n_ctx": 512,
"n_embd": 32,
"n_head": 4,
"n_inner": null,
"n_layer": 5,
"n_positions": 512,
"pad_token_id": 98,
"reorder_and_upcast_attn": false,
"resid_pdrop": 0.1,
"scale_attn_by_inverse_layer_idx": false,
"scale_attn_weights": true,
"summary_activation": null,
"summary_first_dropout": 0.1,
"summary_proj_to_labels": true,
"summary_type": "cls_index",
"summary_use_proj": true,
"torch_dtype": "float32",
"transformers_version": "4.36.2",
"type_vocab_size": 16,
"use_cache": true,
"vocab_size": 1000
}

View File

@@ -0,0 +1,7 @@
{
"_from_model_config": true,
"bos_token_id": 98,
"eos_token_id": 98,
"pad_token_id": 98,
"transformers_version": "4.36.2"
}

View File

@@ -0,0 +1,807 @@
#version: 0.2
Ġ t
h e
Ġ a
i n
Ġt he
e r
o n
Ġ ,
r e
Ġ s
e d
Ġ o
Ġ w
n d
a t
Ġ .
o r
i t
Ġ c
e n
Ġ f
i s
e s
a r
Ġo f
Ġ b
a n
Ġ in
a l
in g
Ġ p
Ġa nd
a s
Ġt o
r o
i c
Ġ m
Ġ d
Ġ h
i on
l e
o u
Ġ T
Ġ re
Ġ =
Ġ "
Ġ A
Ġ S
en t
i l
Ġt h
Ġ 1
s t
Ġ C
e l
o m
Ġ l
a m
Ġ Ċ
Ġ e
Ġ n
Ġ @
a d
a c
Ġw as
Ġ M
u r
ĠT he
e c
Ġ on
l y
Ġ B
Ġ I
Ġ g
Ġ '
e t
o l
i d
i v
i m
Ġf or
i r
- @
Ġ@ -@
i g
o t
t er
Ġa s
Ġ H
u s
o w
Ġs t
u t
it h
a y
Ġ 2
Ġ P
at ion
v er
Ġb e
he r
Ġth at
Ġw ith
Ġ R
c e
t h
Ġ D
Ġ is
u n
e m
Ġ F
Ġw h
u l
Ġb y
Ġa l
c h
Ġ )
Ġ (
Ġ W
Ġc on
r a
Ġ G
o s
Ġ L
Ġ N
Ġa t
er s
c t
Ġ it
Ġ1 9
ro m
a nd
Ġa n
u m
es t
Ġ J
a g
Ġ he
0 0
is t
a in
o d
a v
r i
Ġ E
Ġ O
Ġf rom
Ġc om
Ġh is
o p
Ġp ro
re s
i es
i f
Ġ v
or t
er e
il l
l d
Ġd e
p p
Ġs u
o re
ĠI n
Ġ r
Ġs e
Ġw ere
e w
on g
ig h
ar d
at e
al l
ar t
a k
ic h
Ġc h
Ġo r
a b
an t
u d
o c
b er
Ġe x
g h
it y
at ed
p t
es s
e ar
Ġ K
Ġp l
am e
q u
iv e
ro u
Ġa re
Ġ â
Ġs h
Ġ k
ac k
ec t
Ġâ Ģ
Ġ U
Ġh ad
s e
Ġwh ich
re d
o v
ĠS t
as t
Ġs p
i an
Ġ y
m ent
Ġ le
Ġn ot
g e
or d
r it
i p
in e
el l
al ly
ou r
o st
igh t
t her
a p
Ġ u
is h
ĠC h
ou n
i a
Ġ 3
av e
ar y
u st
o g
Ġ2 00
Ġ un
ou s
ir st
Ġ V
c c
Ġin c
Ġ ;
Ġcom p
r u
ion s
Ġthe ir
Ġb ut
id e
u re
s o
Ġcon t
Ġin t
f ter
ic al
i al
Ġa r
Ġf irst
ou ld
Ġit s
he d
ĠâĢ ĵ
Ġw he
w o
ou t
u b
Ġ2 0
f f
Ġ :
u e
Ġ her
ow n
o k
Ġal so
Ġc l
p er
ig n
at er
r an
or m
i e
om e
or k
as s
i re
e nd
Ġre s
Ġa b
Ġa d
Ġ us
r y
Ġre c
Ġh ave
ag e
ĠH e
Ġ 4
Ġ ro
m er
Ġon e
on d
l ow
Ġh as
ĠT h
d u
Ġ 5
Ġp er
Ġbe en
im e
Ġt wo
en ce
l and
Ġ1 8
. @
Ġ@ .@
ul t
re e
ou gh
i le
Ġwh o
ĠA l
Ġs c
ur ing
p l
or y
it ion
r ic
ation s
Ġd is
Ġth is
Ġb ec
Ġa pp
i z
ĠI t
a re
ac h
l ud
ad e
Ġpl ay
Ġ j
Ġm an
ac t
el y
Ġp art
Ġd es
Ġa g
Ġthe y
Ġy ear
oun t
Ġ20 1
Ġo ver
Ġo ther
ou nd
Ġa fter
i b
o ver
Ġs er
Ġ en
Ġof f
Ġ im
ct ion
Ġ Y
k e
it e
, @
Ġ@ ,@
t e
ur n
Ġinc lud
res s
an ce
an g
Ġat t
ic e
ac e
ar k
Ġo ut
w n
p h
em ber
Ġp re
Ġu p
en s
m an
Ġe v
Ġt ime
nd er
rou gh
c ed
Ġf in
Ġint o
on e
p ort
rou nd
w e
re n
l es
in t
ĠO n
v el
Ġcom m
Ġs he
as on
am p
Ġt e
Ġw ould
w ard
Ġm ore
Ġ 6
i ed
os e
ri b
ĠU n
Ġal l
ing s
ter n
c es
ab le
Ġw e
it ed
e ver
ent s
Ġh im
as ed
or s
o y
o od
Ġc ent
i x
as e
il d
ĠA n
Ġ 7
Ġw ork
at es
i ous
at h
Ġp o
ro p
ol d
al s
is s
e y
ic t
Ġf e
Ġthe m
g an
Ġs ec
Ġb et
Ġwhe n
Ġs ong
Ġre m
e p
f orm
a il
f er
Ġe ar
ub l
a w
Ġk n
ak e
a us
Ġm ost
Ġcon s
Ġd uring
ĠA s
or th
Ġn ew
er ed
il m
v ed
at t
Ġon ly
Ġ 9
Ġd ec
Ġ 8
ic k
Ġg ame
on s
u g
Ġt r
f t
ot h
o ok
ĠM ar
re at
w ay
Ġc an
ol low
ou th
we en
ĠE n
Ġ19 9
ter s
Ġre l
in d
Ġab out
Ġse ason
Ġag ain
r al
Ġth ree
ation al
Ġu nder
ul ar
Ġm e
Ġth an
ĠC om
ĠA r
h ip
o b
Ġn e
Ġbet ween
Ġf l
h n
v e
Ġch ar
Ġc ol
Ġrec ord
i ew
r on
f ore
Ġth rough
is ion
or n
Ġ 00
oc k
Ġ ver
Ġl ater
Ġn um
Ġe nd
ol og
am es
Ġp os
Ġw rit
Ġpro du
Ġwh ile
Ġa ct
Ġre le
Ġf ilm
is hed
Ġp r
an s
Ġre g
Ġfor m
Ġas s
ĠS e
ur y
t ed
t s
Ġm ade
Ġsu b
Ġp e
Ġs o
or ld
Ġre t
ĠN ew
Ġsp ec
Ġa cc
Ġ qu
Ġwhe re
en er
Ġm ov
he s
mer ic
at ing
Ġin ter
ĠL e
ĠA meric
Ġ ra
Ġs ome
Ġc o
Ġl ar
Ġb u
Ġde f
b um
Ġa c
Ġm us
Ġf ollow
ĠA t
in s
iv ed
if ic
u al
Ġa m
Ġsu ch
Ġsec ond
i ke
Ġf our
Ġin d
an n
he n
Ġus ed
ĠR e
ic s
le ct
Ġd ay
i el
il y
ĠTh is
Ġ 0
Ġp ubl
Ġc all
ĠJ o
l l
Ġal bum
Ġ00 0
ran s
Ġd o
an y
Ġbe fore
ro s
ĠS h
Ġs y
a id
ĠEn g
Ġbe ing
Ġ1 0
u c
Ġe p
Ġsu pp
Ġthe re
Ġyear s
ar s
ow ever
Ġ ent
if e
Ġh igh
Ġf ound
ir d
Ġn o
Ġs et
in es
iv er
i o
ot her
j ect
Ġs ur
a j
t en
Ġt ra
Ġ1 2
is ed
it ies
vel op
Ġb l
al e
Ġser ies
Ġl oc
Ġnum ber
Ġp res
an e
aus e
od e
e k
t on
ĠS c
i er
is e
Ġse ver
in ce
Ġb oth
an k
ro w
ire ct
s on
Ġthe n
ĠB rit
i et
Ġ1 6
Ġep is
Ġinclud ing
it s
ig in
p r
Ġ /
Ġagain st
Ġw ell
Ġbec ame
Ġex p
Ġkn own
Ġt rans
Ġchar ac
ĠâĢ Ķ
r am
Ġb ack
Ġad d
Ġp op
Ġg o
ur ch
Ġdes c
Ġs ing
iel d
Ġper form
ain ed
Ġre ce
id ent
Ġe m
er t
u res
Ġin v
Ġde p
Ġ19 8
a ir
er n
at her
f ul
Ġ Z
Ġm on
Ġman y
Ġm ain
Ġst ud
Ġl ong
in n
th ough
u p
o ol
ĠUn ited
l ed
em ent
Ġ1 5
ow er
ĠJo hn
Ġo p
Ġ1 1
in ed
Ġm et
o ber
le y
Ġ1 7
Ġcent ury
Ġte am
Ġ est
ĠA fter
y l
Ġm in
u ch
ut e
Ġde velop
ĠS he
i am
Ġsh ow
el f
Ġre p
Ġcon c
at ive
Ġc re
over n
a red
Ġ19 4
Ġor igin
Ġs m
iv ers
a z
Ġle ad
Ġsever al
a h
Ġo b
Ġre v
Ġm ill
er m
u ally
o ot
Ġbe gan
Ġ19 6
i red
Ġd if
Ġcont in
Ġs ign
i k
ĠI nd
ment s
iz ed
Ġ19 7
Ġd irect
a u
Ġex t
ros s
em b
d er
Ġp ol
Ġm ay
a pt
el s
ĠW h
Ġcomp le
Ġar t
ĠB r
ĠI s
un e
t il
Ġc rit
Ġh ist
Ġear ly
Ġc ould
ĠC on
Ġd id
Ġb el
Ġcall ed
u ed
Ġn ear
Ġepis ode
y p
Ġdesc rib

View File

@@ -0,0 +1,23 @@
{
"bos_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"eos_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"unk_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,19 @@
{
"add_prefix_space": false,
"added_tokens_decoder": {
"0": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
}
},
"bos_token": "<|endoftext|>",
"clean_up_tokenization_spaces": true,
"eos_token": "<|endoftext|>",
"model_max_length": 1024,
"tokenizer_class": "GPT2Tokenizer",
"unk_token": "<|endoftext|>"
}

File diff suppressed because one or more lines are too long