
DSPy의 가장 강력한 기능은 특히 파이프라인 내에서 LM을 사용하는 프로그램을 빌드할 때 LM의 프롬프트(또는 가중치)를 알고리즘적으로 최적화하는 데 중점을 두고 있습니다.
먼저 언어 모델을 설정할 수 있는지 확인해 보겠습니다. DSPy는 다양한 원격 및 로컬 LM에 대한 클라이언트를 지원합니다.
LM 클라이언트 설정하기.
LM에 연결되는 생성자를 호출하면 됩니다. 그런 다음 dspy.configure를 사용하여 이를 기본 LM으로 선언합니다.
예를 들어 OpenAI 언어 모델을 사용하려면 다음과 같이 하면 됩니다.
gpt3_turbo = dspy.OpenAI(model='gpt-3.5-turbo-1106', max_tokens=300)
dspy.configure(lm=gpt3_turbo)
Directly calling the LM.
문자열로 LM을 호출하여 원시 프롬프트, 즉 문자열을 제공할 수 있습니다.
gpt3_turbo("hello! this is a raw prompt to GPT-3.5")
Output:
['Hello! How can I assist you today?']
DSPy 서명과 함께 LM 사용
나머지 가이드에서 자세히 설명하는 DSPy 서명(입력/출력 사양) 및 모듈을 통해 LM을 사용할 수도 있습니다.
# 모듈(ChainOfThought)을 정의하고 서명을 할당합니다(질문이 주어지면 답을 반환합니다).
qa = dspy.ChainOfThought('question -> answer')
# 위의 `dspy.configure`로 구성된 기본 LM으로 실행합니다.
response = qa(question="How many floors are in the castle David Gregory inherited?")
print(response.answer)
Output:
The castle David Gregory inherited has 7 floors.
한 번에 여러 개의 LM을 사용하기.
위의 기본 LM은 GPT-3.5, gpt3_turbo입니다. 예를 들어 GPT-4 또는 LLama-2로 코드를 실행하려면 어떻게 해야 하나요?
기본 LM을 변경하는 대신 코드 블록 내에서 변경하면 됩니다.
Tip: dspy.configure 및 dspy.context를 사용하면 스레드에 안전합니다!
# 위에서 구성한 기본 LM, 즉 GPT-3.5로 실행합니다.
response = qa(question="How many floors are in the castle David Gregory inherited?")
print('GPT-3.5:', response.answer)
gpt4_turbo = dspy.OpenAI(model='gpt-4-1106-preview', max_tokens=300)
# 대신 GPT-4로 실행
with dspy.context(lm=gpt4_turbo):
response = qa(question="How many floors are in the castle David Gregory inherited?")
print('GPT-4-turbo:', response.answer)
Output:
GPT-3.5: The castle David Gregory inherited has 7 floors.
GPT-4-turbo: The number of floors in the castle David Gregory inherited cannot be determined with the information provided.
Tips and Tricks.
DSPy에서는 모든 LM 호출이 캐시됩니다. 동일한 호출을 반복하면 동일한 출력을 얻게 됩니다. (입력이나 구성을 변경하면 새로운 출력을 얻게 됩니다.)
5개의 출력을 생성하려면 모듈 생성자에서 n=5를 사용하거나 모듈을 호출할 때 config=dict(n=5)를 전달하면 됩니다.
qa = dspy.ChainOfThought('question -> answer', n=5)
response = qa(question="How many floors are in the castle David Gregory inherited?")
response.completions.answer
Output:
["The specific number of floors in David Gregory's inherited castle is not provided here, so further research would be needed to determine the answer.",
'The castle David Gregory inherited has 4 floors.',
'The castle David Gregory inherited has 5 floors.',
'David Gregory inherited 10 floors in the castle.',
'The castle David Gregory inherited has 5 floors.']
같은 입력값으로 반복해서 qa(…)를 호출하면 항상 같은 값이 반환됩니다! 이는 의도된 것입니다.
동일한 입력으로 한 번에 하나의 출력을 반복하여 생성하려면 아래와 같이 각 요청이 (약간) 고유한지 확인하여 캐시를 우회합니다.
for idx in range(5):
response = qa(question="How many floors are in the castle David Gregory inherited?",
config=dict(temperature=0.7+0.0001*idx))
print(f'{idx+1}.', response.answer)
Output:
1. The specific number of floors in David Gregory's inherited castle is not provided here, so further research would be needed to determine the answer.
2. It is not possible to determine the exact number of floors in the castle David Gregory inherited without specific information about the castle's layout and history.
3. The castle David Gregory inherited has 5 floors.
4. We need more information to determine the number of floors in the castle David Gregory inherited.
5. The castle David Gregory inherited has a total of 6 floors.
Remote LMs.
이러한 모델은 관리형 서비스입니다. 가입하고 API 키를 받기만 하면 됩니다. 아래의 원격 LM을 호출하면 인증이 완료된 것으로 가정하고 다음 형식에 따라 LM을 설정합니다:
lm = dspy.{provider_listed_below}(model="your model", model_request_kwargs="...")
dspy.OpenAI
for GPT-3.5 and GPT-4.dspy.Cohere
dspy.Anyscale
for hosted Llama2 models.dspy.Together
for hosted various open source models.dspy.PremAI
for hosted best open source and closed source models.
Local LMs.
이러한 모델은 자체 GPU에서 호스팅해야 합니다. 아래에서 이를 수행하는 방법에 대한 지침을 제공합니다.
dspy.HFClientTGI: 텍스트 생성 추론(TGI) 시스템을 통한 허깅페이스 모델용. 튜토리얼: TGI 서버를 설치하고 실행하려면 어떻게 하나요?
tgi_mistral = dspy.HFClientTGI(model="mistralai/Mistral-7B-Instruct-v0.2", port=8080, url="http://localhost")
dspy.HFClientVLLM: vLLM을 통한 허깅페이스 모델용. 튜토리얼: vLLM 서버를 설치하고 실행하려면 어떻게 하나요?
vllm_mistral = dspy.HFClientVLLM(model="mistralai/Mistral-7B-Instruct-v0.2", port=8080, url="http://localhost")
dspy.HFModel (실험용) 튜토리얼: HFModel을 사용하여 모델을 초기화하려면 어떻게 하나요?
mistral = dspy.HFModel(model = 'mistralai/Mistral-7B-Instruct-v0.2')
dspy.Ollama(실험적)를 통해 오픈 소스 모델에 대한 Ollama. 튜토리얼: 로컬 컴퓨터에 Ollama를 설치하고 사용하려면 어떻게 하나요?
ollama_mistral = dspy.OllamaLocal(model='mistral')
dspy.ChatModuleClient (실험적): MLC를 어떻게 설치하고 사용하나요?
model = 'dist/prebuilt/mlc-chat-Llama-2-7b-chat-hf-q4f16_1'
model_path = 'dist/prebuilt/lib/Llama-2-7b-chat-hf-q4f16_1-cuda.so'
llama = dspy.ChatModuleClient(model=model, model_path=model_path)