LLM을 사용하여 전략 패턴 구현.
또한 미래에 이것이 중요한 이유에 대한 더 넓은 관점은 https://blog.blackhc.net/2022/12/llm_software_engineering/을 참조하십시오.
이 패키지는 LLM (예 : OpenAi의 GPT-3)에 연결되는 데코레이터 llm_strategy 추가하고 LLM을 사용하여 인터페이스 클래스에서 초록 메소드를 "구현"합니다. LLM에 요청을 전달하고 Python의 @dataclasses 사용하여 응답을 Python 데이터로 다시 변환하여이를 수행합니다.
Doc 문자열, 유형 주석 및 메소드/함수 이름을 LLM의 프롬프트로 사용하고 결과를 다시 Python 유형으로 자동 변환 할 수 있습니다 (현재 @dataclasses 만 지원). 또한 해석을 위해 LLM으로 전송하기 위해 데이터 스키마를 추출 할 수도 있습니다. llm-strategy 패키지는 여전히 일부 Python 코드에 의존하지만, 추가로 저렴한 LLM을 사용하여 구조화 된 데이터의 구문 분석을 자동화하여 향후이 코드의 필요성을 줄일 수 있습니다.
최신 버전에는 하이퍼 파라미터 추적 및 LLM의 흔적 수집을위한 패키지도 포함되어 있습니다.
예를 들어, 메타 최적화가 가능합니다. 제네릭을 사용한 간단한 구현은 예/연구를 참조하십시오.
예제 WANDB 트레이스를 찾을 수 있습니다. https://wandb.ai/blackhc/blackboard-pagi/reports/meta-optimization-example-trace-vmlldzo3mdmxodez?accesstoken=p9hubfskmq1z5yj1uz7wx1idh304diiernp7pjlrjlrjrjrjrjrjlwvpaozlwv3dnitjlnitjlrjlrjrjlrjlrjlwvpaozlwv3dnitjlnit
제네릭을 사용하여 패턴을 보여주는 프롬프트는 간단합니다.
T_TaskParameters = TypeVar ( "T_TaskParameters" )
T_TaskResults = TypeVar ( "T_TaskResults" )
T_Hyperparameters = TypeVar ( "T_Hyperparameters" )
class TaskRun ( GenericModel , Generic [ T_TaskParameters , T_TaskResults , T_Hyperparameters ]):
"""
The task run. This is the 'data' we use to optimize the hyperparameters.
"""
task_parameters : T_TaskParameters = Field (..., description = "The task parameters." )
hyperparameters : T_Hyperparameters = Field (
...,
description = "The hyperparameters used for the task. We optimize these." ,
)
all_chat_chains : dict = Field (..., description = "The chat chains from the task execution." )
return_value : T_TaskResults | None = Field (
..., description = "The results of the task. (None for exceptions/failure.)"
)
exception : list [ str ] | str | None = Field (..., description = "Exception that occurred during the task execution." )
class TaskReflection ( BaseModel ):
"""
The reflections on the task.
This contains the lessons we learn from each task run to come up with better
hyperparameters to try.
"""
feedback : str = Field (
...,
description = (
"Only look at the final results field. Does its content satisfy the "
"task description and task parameters? Does it contain all the relevant "
"information from the all_chains and all_prompts fields? What could be improved "
"in the results?"
),
)
evaluation : str = Field (
...,
description = (
"The evaluation of the outputs given the task. Is the output satisfying? What is wrong? What is missing?"
),
)
hyperparameter_suggestion : str = Field (
...,
description = "How we want to change the hyperparameters to improve the results. What could we try to change?" ,
)
hyperparameter_missing : str = Field (
...,
description = (
"What hyperparameters are missing to improve the results? What could "
"be changed that is not exposed via hyperparameters?"
),
)
class TaskInfo ( GenericModel , Generic [ T_TaskParameters , T_TaskResults , T_Hyperparameters ]):
"""
The task run and the reflection on the experiment.
"""
task_parameters : T_TaskParameters = Field (..., description = "The task parameters." )
hyperparameters : T_Hyperparameters = Field (
...,
description = "The hyperparameters used for the task. We optimize these." ,
)
reflection : TaskReflection = Field (..., description = "The reflection on the task." )
class OptimizationInfo ( GenericModel , Generic [ T_TaskParameters , T_TaskResults , T_Hyperparameters ]):
"""
The optimization information. This is the data we use to optimize the
hyperparameters.
"""
older_task_summary : str | None = Field (
None ,
description = (
"A summary of previous experiments and the proposed changes with "
"the goal of avoiding trying the same changes repeatedly."
),
)
task_infos : list [ TaskInfo [ T_TaskParameters , T_TaskResults , T_Hyperparameters ]] = Field (
..., description = "The most recent tasks we have run and our reflections on them."
)
best_hyperparameters : T_Hyperparameters = Field (..., description = "The best hyperparameters we have found so far." )
class OptimizationStep ( GenericModel , Generic [ T_TaskParameters , T_TaskResults , T_Hyperparameters ]):
"""
The next optimization steps. New hyperparameters we want to try experiments and new
task parameters we want to evaluate on given the previous experiments.
"""
best_hyperparameters : T_Hyperparameters = Field (
...,
description = "The best hyperparameters we have found so far given task_infos and history." ,
)
suggestion : str = Field (
...,
description = (
"The suggestions for the next experiments. What could we try to "
"change? We will try several tasks next and several sets of hyperparameters. "
"Let's think step by step."
),
)
task_parameters_suggestions : list [ T_TaskParameters ] = Field (
...,
description = "The task parameters we want to try next." ,
hint_min_items = 1 ,
hint_max_items = 4 ,
)
hyperparameter_suggestions : list [ T_Hyperparameters ] = Field (
...,
description = "The hyperparameters we want to try next." ,
hint_min_items = 1 ,
hint_max_items = 2 ,
)
class ImprovementProbability ( BaseModel ):
considerations : list [ str ] = Field (..., description = "The considerations for potential improvements." )
probability : float = Field (..., description = "The probability of improvement." )
class LLMOptimizer :
@ llm_explicit_function
@ staticmethod
def reflect_on_task_run (
language_model ,
task_run : TaskRun [ T_TaskParameters , T_TaskResults , T_Hyperparameters ],
) -> TaskReflection :
"""
Reflect on the results given the task parameters and hyperparameters.
This contains the lessons we learn from each task run to come up with better
hyperparameters to try.
"""
raise NotImplementedError ()
@ llm_explicit_function
@ staticmethod
def summarize_optimization_info (
language_model ,
optimization_info : OptimizationInfo [ T_TaskParameters , T_TaskResults , T_Hyperparameters ],
) -> str :
"""
Summarize the optimization info. We want to preserve all relevant knowledge for
improving the hyperparameters in the future. All information from previous
experiments will be forgotten except for what this summary.
"""
raise NotImplementedError ()
@ llm_explicit_function
@ staticmethod
def suggest_next_optimization_step (
language_model ,
optimization_info : OptimizationInfo [ T_TaskParameters , T_TaskResults , T_Hyperparameters ],
) -> OptimizationStep [ T_TaskParameters , T_TaskResults , T_Hyperparameters ]:
"""
Suggest the next optimization step.
"""
raise NotImplementedError ()
@ llm_explicit_function
@ staticmethod
def probability_for_improvement (
language_model ,
optimization_info : OptimizationInfo [ T_TaskParameters , T_TaskResults , T_Hyperparameters ],
) -> ImprovementProbability :
"""
Return the probability for improvement (between 0 and 1).
This is your confidence that your next optimization steps will improve the
hyperparameters given the information provided. If you think that the
information available is unlikely to lead to better hyperparameters, return 0.
If you think that the information available is very likely to lead to better
hyperparameters, return 1. Be concise.
"""
raise NotImplementedError () from dataclasses import dataclass
from llm_strategy import llm_strategy
from langchain . llms import OpenAI
@ llm_strategy ( OpenAI ( max_tokens = 256 ))
@ dataclass
class Customer :
key : str
first_name : str
last_name : str
birthdate : str
address : str
@ property
def age ( self ) -> int :
"""Return the current age of the customer.
This is a computed property based on `birthdate` and the current year (2022).
"""
raise NotImplementedError ()
@ dataclass
class CustomerDatabase :
customers : list [ Customer ]
def find_customer_key ( self , query : str ) -> list [ str ]:
"""Find the keys of the customers that match a natural language query best (sorted by closeness to the match).
We support semantic queries instead of SQL, so we can search for things like
"the customer that was born in 1990".
Args:
query: Natural language query
Returns:
The index of the best matching customer in the database.
"""
raise NotImplementedError ()
def load ( self ):
"""Load the customer database from a file."""
raise NotImplementedError ()
def store ( self ):
"""Store the customer database to a file."""
raise NotImplementedError ()
@ llm_strategy ( OpenAI ( max_tokens = 1024 ))
@ dataclass
class MockCustomerDatabase ( CustomerDatabase ):
def load ( self ):
self . customers = self . create_mock_customers ( 10 )
def store ( self ):
pass
@ staticmethod
def create_mock_customers ( num_customers : int = 1 ) -> list [ Customer ]:
"""
Create mock customers with believable data (our customers are world citizens).
"""
raise NotImplementedError ()전체 예를 들어 예제/customer_database_search.py를 참조하십시오.
먼저 저장소를 복제하십시오. 그런 다음 환경과 사전 커밋 후크를 설치하십시오
make installCI/CD 파이프 라인은 풀 요청을 열거나 메인으로 병합되거나 새 릴리스를 만들 때 트리거됩니다.
PYPI 또는 Artifactory에 게시하기위한 설정을 마무리하려면 여기를 참조하십시오. MKDocs로 자동 문서를 활성화하려면 여기를 참조하십시오. 코드 커버리지 보고서를 활성화하려면 여기를 참조하십시오.
PYPI_TOKEN 이라는 이름으로 프로젝트 비밀에 API 토큰을 추가하십시오.*.*.* 에서 새 태그를 만듭니다.자세한 내용은 여기를 참조하십시오.
FPGMAAS/Cookiecutter-Poetry로 시작된 저장소.