Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Demontego/dspy into fix-marqo
Browse files Browse the repository at this point in the history
  • Loading branch information
Demontego committed Mar 12, 2024
2 parents cad9b47 + 2d845de commit 7d70c8a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
10 changes: 6 additions & 4 deletions docs/api/modules/ProgramOfThought.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,18 @@ Main method to execute the code generation and refinement process.

```python
#Define a simple signature for basic question answering
generate_answer_signature = dspy.Signature("question -> answer")
generate_answer_signature.attach(question=("Question:", "")).attach(answer=("Answer:", "often between 1 and 5 words"))
class GenerateAnswer(dspy.Signature):
"""Answer questions with short factoid answers."""
question = dspy.InputField()
answer = dspy.OutputField(desc="often between 1 and 5 words")

# Pass signature to ProgramOfThought Module
pot = dspy.ProgramOfThought(generate_answer_signature)
pot = dspy.ProgramOfThought(GenerateAnswer)

#Call the ProgramOfThought module on a particular input
question = 'Sarah has 5 apples. She buys 7 more apples from the store. How many apples does Sarah have now?'
result = pot(question=question)

print(f"Question: {question}")
print(f"Final Predicted Answer (after ProgramOfThought process): {result.answer}")
```
```
4 changes: 2 additions & 2 deletions docs/api/retrieval_model_clients/Neo4jRM.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ retriever_model = Neo4jRM(

results = retriever_model("Explore the significance of quantum computing", k=3)

for passage in results.passages:
print("Document:", result, "\n")
for passage in results:
print("Document:", passage, "\n")
```
31 changes: 18 additions & 13 deletions docs/docs/building-blocks/8-typed_predictors.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ Let's take a simple task as an example i.e. given the `context` and `query`, the
from pydantic import BaseModel, Field

class Input(BaseModel):
context: str = Field(..., description="The context for the question")
query: str = Field(..., description="The question to be answered")
context: str = Field(description="The context for the question")
query: str = Field(description="The question to be answered")

class Output(BaseModel):
answer: str = Field(..., description="The answer for the question")
factual_: float = Field(..., description="The confidence score for the answer")
answer: str = Field(description="The answer for the question")
confidence: float = Field(ge=0, le=1, description="The confidence score for the answer")
```

As you can see, we can describe the attributes by defining a simple Signature that takes in the input and returns the output.
Expand All @@ -46,6 +46,11 @@ predictor = dspy.TypedPredictor(QASignature)

Similar to other modules, we pass the `QASignature` to `dspy.TypedPredictor` which enforces the typed constraints.

And similarly to `dspy.Predict`, we can also use a "string signature", which we type as:
```python
predictor = dspy.TypedPredictor("input:Input -> output:Output")
```

### I/O in Typed Predictors

Now let's test out the Typed Predictor by providing some sample input to the predictor and verifying the output type. We can create an `Input` instance and pass it to the predictor to get a dictionary of the output.
Expand All @@ -62,8 +67,8 @@ prediction = predictor(input=doc_query_pair)
Let's see the output and its type.

```python
answer = prediction['answer']
confidence_score = prediction['confidence_score']
answer = prediction.answer
confidence_score = prediction.confidence

print(f"Prediction: {prediction}\n\n")
print(f"Answer: {answer}, Answer Type: {type(answer)}")
Expand All @@ -89,18 +94,18 @@ prediction = cot_predictor(input=doc_query_pair)

While the `dspy.TypedPredictor` and `dspy.TypedChainOfThought` provide a convenient way to use typed predictors, you can also use them as decorators to enforce type constraints on the inputs and outputs of the function. This relies on the internal definitions of the Signature class and its function arguments, outputs, and docstrings.

```
# Function name is output key
```python
@dspy.predictor
def qa_function(doc_query_pair: Input) -> Output:
"""Answer the question based on the context and query provided, and on the scale of 10 tell how confident you are about the answer."""
def answer(doc_query_pair: Input) -> Output:
"""Answer the question based on the context and query provided, and on the scale of 0-1 tell how confident you are about the answer."""
pass

@dspy.cot
def qa_function(doc_query_pair: Input) -> Output:
"""Answer the question based on the context and query provided, and on the scale of 10 tell how confident you are about the answer."""
def answer(doc_query_pair: Input) -> Output:
"""Answer the question based on the context and query provided, and on the scale of 0-1 tell how confident you are about the answer."""
pass

prediction = answer(doc_query_pair=doc_query_pair)
```

## Composing Functional Typed Predictors in `dspy.Module`
Expand Down
3 changes: 2 additions & 1 deletion examples/qa/hotpot/hotpotqa_with_MIPRO.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"id": "3wEDck3ZqZH0"
},
"source": [
"# Using __<ins>M</ins>ulti-stage <ins>I</ins>nstruction <ins>P</ins>roposal & <ins>O</ins>ptimization (MIPRO)__ in DSPy"
"# Using __<ins>M</ins>ulti-stage <ins>I</ins>nstruction <ins>P</ins>roposal & <ins>O</ins>ptimization (MIPRO)__ in DSPy\n",
"[![colab-badge](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/examples/qa/hotpot/hotpotqa_with_MIPRO.ipynb)"
]
},
{
Expand Down

0 comments on commit 7d70c8a

Please sign in to comment.