lm=dspy.LM("openai/meta-llama/Llama-3.1-8B-Instruct",api_base="http://localhost:7501/v1",# ensure this points to your portapi_key="local",model_type='chat')dspy.configure(lm=lm)
lm("Say this is a test!",temperature=0.7)# => ['This is a test!']lm(messages=[{"role":"user","content":"Say this is a test!"}])# => ['This is a test!']
DSPy 将您的注意力从调整提示字符串转移到使用结构化和声明性自然语言模块进行编程。对于系统中的每个 AI 组件,您可以将输入/输出行为指定为签名,并选择一个模块来分配调用 LM 的策略。DSPy 将您的签名扩展为提示并解析您的类型化输出,因此您可以将不同的模块组合在一起,形成符合人体工程学、可移植和可优化的 AI 系统。
defsearch_wikipedia(query:str)->list[str]:results=dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')(query,k=3)return[x['text']forxinresults]rag=dspy.ChainOfThought('context, question -> response')question="What's the name of the castle that David Gregory inherited?"rag(context=search_wikipedia(question),question=question)
fromtypingimportLiteralclassClassify(dspy.Signature):"""Classify sentiment of a given sentence."""sentence:str=dspy.InputField()sentiment:Literal['positive','negative','neutral']=dspy.OutputField()confidence:float=dspy.OutputField()classify=dspy.Predict(Classify)classify(sentence="This book was super fun to read, though not the last chapter.")
classExtractInfo(dspy.Signature):"""Extract structured information from text."""text:str=dspy.InputField()title:str=dspy.OutputField()headings:list[str]=dspy.OutputField()entities:list[dict[str,str]]=dspy.OutputField(desc="a list of entities and their metadata")module=dspy.Predict(ExtractInfo)text="Apple Inc. announced its latest iPhone 14 today." \
"The CEO, Tim Cook, highlighted its new features in a press release."response=module(text=text)print(response.title)print(response.headings)print(response.entities)
defevaluate_math(expression:str):returndspy.PythonInterpreter({}).execute(expression)defsearch_wikipedia(query:str):results=dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')(query,k=3)return[x['text']forxinresults]react=dspy.ReAct("question -> answer: float",tools=[evaluate_math,search_wikipedia])pred=react(question="What is 9362158 divided by the year of birth of David Gregory of Kinnairdy castle?")print(pred.answer)
classOutline(dspy.Signature):"""Outline a thorough overview of a topic."""topic:str=dspy.InputField()title:str=dspy.OutputField()sections:list[str]=dspy.OutputField()section_subheadings:dict[str,list[str]]=dspy.OutputField(desc="mapping from section headings to subheadings")classDraftSection(dspy.Signature):"""Draft a top-level section of an article."""topic:str=dspy.InputField()section_heading:str=dspy.InputField()section_subheadings:list[str]=dspy.InputField()content:str=dspy.OutputField(desc="markdown-formatted section")classDraftArticle(dspy.Module):def__init__(self):self.build_outline=dspy.ChainOfThought(Outline)self.draft_section=dspy.ChainOfThought(DraftSection)defforward(self,topic):outline=self.build_outline(topic=topic)sections=[]forheading,subheadingsinoutline.section_subheadings.items():section,subheadings=f"## {heading}",[f"### {subheading}"forsubheadinginsubheadings]section=self.draft_section(topic=outline.title,section_heading=section,section_subheadings=subheadings)sections.append(section.content)returndspy.Prediction(title=outline.title,sections=sections)draft_article=DraftArticle()article=draft_article(topic="World Cup 2002")
可能的输出:
一篇1500字的关于该主题的文章,例如。
## Qualification Process
The qualification process for the 2002 FIFA World Cup involved a series of..... [shortened here for presentation].
### UEFA Qualifiers
The UEFA qualifiers involved 50 teams competing for 13..... [shortened here for presentation].
.... [rest of the article]
classRAG(dspy.Module):def__init__(self,num_docs=5):self.num_docs=num_docsself.respond=dspy.ChainOfThought('context, question -> response')defforward(self,question):context=search(question,k=self.num_docs)# defined in tutorial linked belowreturnself.respond(context=context,question=question)tp=dspy.MIPROv2(metric=dspy.evaluate.SemanticF1(decompositional=True),auto="medium",num_threads=24)optimized_rag=tp.compile(RAG(),trainset=trainset,max_bootstrapped_demos=2,max_labeled_demos=2)
importdspydspy.configure(lm=dspy.LM('gpt-4o-mini-2024-07-18'))# Define the DSPy module for classification. It will use the hint at training time, if available.signature=dspy.Signature("text -> label").with_updated_fields('label',type_=Literal[tuple(CLASSES)])classify=dspy.ChainOfThoughtWithHint(signature)# Optimize via BootstrapFinetune.optimizer=dspy.BootstrapFinetune(metric=(lambdax,y,trace=None:x.label==y.label),num_threads=24)optimized=optimizer.compile(classify,trainset=trainset)optimized_classifier(text="What does a pending cash withdrawal mean?")