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!']
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)
importrandomfromtypingimportLiteralfromdatasetsimportload_datasetimportdspyfromdspy.datasetsimportDataLoader# Load the Banking77 dataset.CLASSES=load_dataset("PolyAI/banking77",split="train",trust_remote_code=True).features["label"].nameskwargs={"fields":("text","label"),"input_keys":("text",),"split":"train","trust_remote_code":True}# Load the first 2000 examples from the dataset, and assign a hint to each *training* example.trainset=[dspy.Example(x,hint=CLASSES[x.label],label=CLASSES[x.label]).with_inputs("text","hint")forxinDataLoader().from_huggingface(dataset_name="PolyAI/banking77",**kwargs)[:2000]]random.Random(0).shuffle(trainset)
importdspylm=dspy.LM('openai/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, hint -> label").with_updated_fields("label",type_=Literal[tuple(CLASSES)])classify=dspy.ChainOfThought(signature)classify.set_lm(lm)# 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(text="What does a pending cash withdrawal mean?")# For a complete fine-tuning tutorial, see: https://dspy.ai/tutorials/classification_finetuning/