from langsmith import wrappers, Client
from openai import AsyncOpenAI
# 선택사항: 모든 모델 호출을 추적하기 위해 OpenAI 클라이언트를 래핑합니다.
oai_client = wrappers.wrap_openai(AsyncOpenAI())
# 선택사항: 이 함수의 입력/출력을 추적하기 위해 'traceable' 데코레이터를 추가합니다.
@traceable
async def researcher_app(inputs: dict) -> str:
instructions = """You are an excellent researcher. Given a high-level research idea, \
list 5 concrete questions that should be investigated to determine if the idea is worth pursuing."""
response = await oai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": instructions},
{"role": "user", "content": inputs["idea"]},
],
)
return response.choices[0].message.content
# 평가자 함수는 동기 또는 비동기일 수 있습니다
def concise(inputs: dict, outputs: dict) -> bool:
return len(outputs["output"]) < 3 * len(inputs["idea"])
ls_client = Client()
ideas = [
"universal basic income",
"nuclear fusion",
"hyperloop",
"nuclear powered rockets",
]
dataset = ls_client.create_dataset("research ideas")
ls_client.create_examples(
dataset_name=dataset.name,
examples=[{"inputs": {"idea": i}} for i in ideas],
)
# 'aevaluate' 함수를 직접 사용할 수도 있습니다:
# from langsmith import aevaluate
# await aevaluate(...)
results = await ls_client.aevaluate(
researcher_app,
data=dataset,
evaluators=[concise],
# 선택사항: 동시성을 추가합니다.
max_concurrency=2, # 선택사항: 동시성을 추가합니다.
experiment_prefix="gpt-4o-mini-baseline" # 선택사항: 기본적으로 무작위입니다.
)