DSPy is a framework for programming language models rather than relying on static prompts. It enables you to build modular AI systems with code instead of hand-crafted prompting, and it offers methods to automatically optimize these systems.Features
Programmatic approach to LLM interactions through Python
Modular components for building complex AI pipelines
Self-improvement algorithms that optimize prompts and weights
Support for various applications from simple classifiers to RAG systems and agent loops
import dspy#Configure dspy with a LLM from Together AIlm = dspy.LM('together_ai/togethercomputer/llama-2-70b-chat', api_key=os.environ.get("TOGETHER_API_KEY"), api_base="https://api.together.xyz/v1")#now you can call the LLM directly as followslm("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!']
Now we can set up a DSPy module, like dspy.ReAct with a task-specific signature. For example, question -> answer: float tells the module to take a question and to produce a floating point number answer below.
Copy
Ask AI
#Configure dspy to use the LLMdspy.configure(lm=lm)# Gives the agent access to a python interpreterdef evaluate_math(expression: str): return dspy.PythonInterpreter({}).execute(expression)# Gives the agent access to a wikipedia search tooldef search_wikipedia(query: str): results = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')(query, k=3) return [x['text'] for x in results]# setup ReAct module with question and math answer signaturereact = 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)