Code Monkey home page Code Monkey logo

Comments (1)

sweep-ai avatar sweep-ai commented on July 22, 2024

πŸš€ Here's the PR! #48

See Sweep's progress at the progress dashboard!
πŸ’Ž Sweep Pro: I'm using GPT-4. You have unlimited GPT-4 tickets. (tracking ID: e367926147)

Actions (click)

  • ↻ Restart Sweep

Sandbox Execution βœ“

Here are the sandbox execution logs prior to making any changes:

Sandbox logs for c7a522a
Checking docs/modules/modules.md for syntax errors... βœ… docs/modules/modules.md has no syntax errors! 1/1 βœ“
Checking docs/modules/modules.md for syntax errors...
βœ… docs/modules/modules.md has no syntax errors!

Sandbox passed on the latest main, so sandbox checks will be enabled for this issue.


Step 1: πŸ”Ž Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description.

from dspy.predict.parameter import Parameter
from dspy.primitives.prediction import Prediction
from dspy.signatures.field import InputField, OutputField
from dspy.signatures.signature import infer_prefix
class Predict(Parameter):
def __init__(self, signature, **config):
self.stage = random.randbytes(8).hex()
self.signature = signature #.signature
self.config = config
self.reset()
# if the signature is a string
if isinstance(signature, str):
inputs, outputs = signature.split("->")
inputs, outputs = inputs.split(","), outputs.split(",")
inputs, outputs = [field.strip() for field in inputs], [field.strip() for field in outputs]
assert all(len(field.split()) == 1 for field in (inputs + outputs))
inputs_ = ', '.join([f"`{field}`" for field in inputs])
outputs_ = ', '.join([f"`{field}`" for field in outputs])
instructions = f"""Given the fields {inputs_}, produce the fields {outputs_}."""
inputs = {k: InputField() for k in inputs}
outputs = {k: OutputField() for k in outputs}
for k, v in inputs.items():
v.finalize(k, infer_prefix(k))
for k, v in outputs.items():
v.finalize(k, infer_prefix(k))
self.signature = dsp.Template(instructions, **inputs, **outputs)
def reset(self):
self.lm = None
self.traces = []
self.train = []
self.demos = []
def dump_state(self):
state_keys = ["lm", "traces", "train", "demos"]
return {k: getattr(self, k) for k in state_keys}
def load_state(self, state):
for name, value in state.items():
setattr(self, name, value)
import dspy
self.demos = [dspy.Example(**x) for x in self.demos]
def __call__(self, **kwargs):
return self.forward(**kwargs)
def forward(self, **kwargs):
# Extract the three privileged keyword arguments.
signature = kwargs.pop("signature", self.signature)
demos = kwargs.pop("demos", self.demos)
config = dict(**self.config, **kwargs.pop("config", {}))
# Get the right LM to use.
lm = kwargs.pop("lm", self.lm) or dsp.settings.lm
# If temperature is 0.0 but its n > 1, set temperature to 0.7.
temperature = config.get("temperature", None)
temperature = lm.kwargs['temperature'] if temperature is None else temperature
num_generations = config.get("n", None)
num_generations = lm.kwargs['n'] if num_generations is None else num_generations
if (temperature is None or temperature <= 0.15) and num_generations > 1:
config["temperature"] = 0.7
# print(f"#> Setting temperature to 0.7 since n={num_generations} and prior temperature={temperature}.")
# All of the other kwargs are presumed to fit a prefix of the signature.
x = dsp.Example(demos=demos, **kwargs)
if self.lm is None:
x, C = dsp.generate(signature, **config)(x, stage=self.stage)
else:
with dsp.settings.context(lm=self.lm, query_only=True):
# print(f"using lm = {self.lm} !")
x, C = dsp.generate(signature, **config)(x, stage=self.stage)
completions = []
for c in C:
completions.append({})
for field in signature.fields:
if field.output_variable not in kwargs.keys():
completions[-1][field.output_variable] = getattr(c, field.output_variable)
pred = Prediction.from_completions(completions, signature=signature)
if dsp.settings.trace is not None:
trace = dsp.settings.trace
trace.append((self, {**kwargs}, pred))
return pred
def update_config(self, **kwargs):
self.config = {**self.config, **kwargs}
def get_config(self):
return self.config
def __repr__(self):
return f"{self.__class__.__name__}({self.signature})"
# TODO: get some defaults during init from the context window?
# # TODO: FIXME: Hmm, I guess expected behavior is that contexts can
# affect exeuction. Well, we need to determine whether context dominates, __init__ demoninates, or forward dominates.
# Generally, unless overwritten, we'd see n=None, temperature=None.

dspy/README.md

Lines 1 to 45 in c7a522a

current docs link: https://dspy-ai.readthedocs.io/en/latest/
<p align="center">
<img align="center" src="docs/images/DSPy8.png" width="460px" />
</p>
<p align="left">
## DSPy: _Programming_β€”not promptingβ€”Foundation Models
Paper β€”β€” **[DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines](https://arxiv.org/abs/2310.03714)**
[<img align="center" src="https://colab.research.google.com/assets/colab-badge.svg" />](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/intro.ipynb)
**DSPy** is the framework for solving advanced tasks with language models (LMs) and retrieval models (RMs). **DSPy** unifies techniques for **prompting** and **fine-tuning** LMs β€” and approaches for **reasoning**, **self-improvement**, and **augmentation with retrieval and tools**. All of these are expressed through modules that compose and learn.
To make this possible:
- **DSPy** provides **composable and declarative modules** for instructing LMs in a familiar Pythonic syntax. It upgrades "prompting techniques" like chain-of-thought and self-reflection from hand-adapted _string manipulation tricks_ into truly modular _generalized operations that learn to adapt to your task_.
- **DSPy** introduces an **automatic compiler that teaches LMs** how to conduct the declarative steps in your program. Specifically, the **DSPy compiler** will internally _trace_ your program and then **craft high-quality prompts for large LMs (or train automatic finetunes for small LMs)** to teach them the steps of your task.
The **DSPy compiler** _bootstraps_ prompts and finetunes from minimal data **without needing manual labels for the intermediate steps** in your program. Instead of brittle "prompt engineering" with hacky string manipulation, you can explore a systematic space of modular and trainable pieces.
For complex tasks, **DSPy** can routinely teach powerful models like `GPT-3.5` and local models like `T5-base` or `Llama2-13b` to be much more reliable at tasks. **DSPy** will compile the _same program_ into different few-shot prompts and/or finetunes for each LM.
If you want to see **DSPy** in action, **[open our intro tutorial notebook](intro.ipynb)**.
### Table of Contents
1. **[Installation](#1-installation)**
1. **[Framework Syntax](#2-syntax-youre-in-charge-of-the-workflowits-free-form-python-code)**
1. **[Compiling: Two Powerful Concepts](#3-two-powerful-concepts-signatures--teleprompters)**
1. **[Tutorials & Documentation](#4-documentation--tutorials)**
1. **[FAQ: Is DSPy right for me?](#5-faq-is-dspy-right-for-me)**
### Analogy to Neural Networks
When we build neural networks, we don't write manual _for-loops_ over lists of _hand-tuned_ floats. Instead, you might use a framework like [PyTorch](https://pytorch.org/) to compose declarative layers (e.g., `Convolution` or `Dropout`) and then use optimizers (e.g., SGD or Adam) to learn the parameters of the network.

from .predict import Predict
from .chain_of_thought import ChainOfThought
from .multi_chain_comparison import MultiChainComparison
from .chain_of_thought_with_hint import ChainOfThoughtWithHint
from .react import ReAct
from .aggregation import majority
from .program_of_thought import ProgramOfThought

# dspy.Modules Documentation
This documentation provides an overview of the DSPy Modules.
## DSPy Modules
| Module | Jump To |
| --- | --- |
| Predict | [Predict Section](#dspypredict) |
| Retrieve | [Retrieve Section](#dspyretrieve) |
| ChainOfThought | [ChainOfThought Section](#dspychainofthought) |
| ChainOfThoughtWithHint | [ChainOfThoughtWithHint Section](#dspychainofthoughtwithhint) |
| MultiChainComparison | [MultiChainComparison Section](#dspymultichaincomparison) |
| ReAct | [ReAct Section](#dspyreact) |
| Assertion Helpers | [Assertion Helpers Section](#dspyassertionhelpers) |
## dspy.Predict
### Constructor


Step 2: ⌨️ Coding

Modify docs/modules/modules.md with contents:
β€’ Start by adding a brief introduction to the `predict` module. This should explain what the module is and what it is used for. This information can be drawn from the README.md file.
β€’ Next, describe the code in the `predict` module. This should include a description of the `Predict` class and its methods. Refer to the `dspy/predict/predict.py` file for this information.
β€’ Then, explain the philosophical usage/purpose of `predict`. This should include an explanation of how the `predict` module fits into the overall framework of DSPy and how it contributes to the goal of programming with foundation models. This information can be drawn from the README.md file.
β€’ Finally, provide examples of how to use the `predict` module. These examples can be drawn from the jupyter notebooks. Make sure to explain what each example is doing and how it demonstrates the use of the `predict` module.
--- 
+++ 
@@ -1,4 +1,6 @@
-# dspy.Modules Documentation
+## Predict
+
+The `Predict` module is a core component of DSPy that encapsulates the task-oriented capacities of language models within an accessible interface. It is designed to facilitate the making of predictions and generations based on provided input as part of a broader declarative programming model. The `Predict` module leverages language compositions that signal an intent to generate specific kinds of information or complete complex tasks, effectively bridging the gap between human-level task descriptions and machine-executable code.# dspy.Modules Documentation
 
 This documentation provides an overview of the DSPy Modules.
 
@@ -16,6 +18,10 @@
 
 ## dspy.Predict
 
+### Introduction
+
+The `Predict` class is integral to the DSPy predictive module, responsible for handling predictions and generative tasks. It is constructed with flexibility in mind, allowing users to define the input-output schema through a `signature` and configure the predictive behavior with additional options. The class instills the power of language models into routine programming practices.
+
 ### Constructor
 
 The constructor initializes the `Predict` class and sets up its attributes, taking in the `signature` and additional config options. If the `signature` is a string, it processes the input and output fields, generates instructions, and creates a template for the specified `signature` type.
@@ -41,7 +47,27 @@
 This handler is used to bypass assertions and suggestions. When used, both assertions and suggestions will become no-operations (noops).
 
 #### `bypass_suggest_handler(func)`
-
+### Examples
+
+#### Example 1: Basic Prediction
+
+```python
+# Define the signature of the task
+class MyTaskSignature(dspy.Signature):
+    input1 = dspy.InputField(desc='A description of input1')
+    output1 = dspy.OutputField(desc='Expected output from the prediction')
+
+# Initialize the Predict module with the defined signature
+predictor = dspy.Predict(MyTaskSignature)
+
+# Call the Predict module
+result = predictor(input1='sample input')
+print(f"Predicted output: {result.output1}")
+```
+
+In this example, the `Predict` module is instantiated with a user-defined signature, and a prediction is made through a straightforward call using sample input. It demonstrates the ease of use of `Predict` for task executions based on varying inputs.### Philosophy and Usage
+
+In DSPy, the philosophy around 'predict' entails harnessing the declarative power of language models to execute well-defined tasks within a broader computational context. The 'Predict' module, in essence, transforms these declarative expressions into performative actions that a language model can understand and execute, making it a philosophical cornerstone for high-level abstraction of foundation models. It aligns closely with the framework's goal of making foundation models programmable while retaining flexibility and adaptability to various task specifications and domains.
 This handler is used to bypass suggestions only. If a suggestion fails, it will be logged but not raised. If an assertion fails, it will be raised.
 
 #### `bypass_assert_handler(func)`
  • Running GitHub Actions for docs/modules/modules.md βœ“ Edit
Check docs/modules/modules.md with contents:

Ran GitHub Actions for ee3fc122a66e85aab1627f48c344119435b11946:


Step 3: πŸ” Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/ensure_predict_in_the_dspy_folder_has_do.


πŸŽ‰ Latest improvements to Sweep:

  • We just released a dashboard to track Sweep's progress on your issue in real-time, showing every stage of the process – from search to planning and coding.
  • Sweep uses OpenAI's latest Assistant API to plan code changes and modify code! This is 3x faster and significantly more reliable as it allows Sweep to edit code and validate the changes in tight iterations, the same way as a human would.
  • Try using the GitHub issues extension to create Sweep issues directly from your editor! GitHub Issues and Pull Requests.

πŸ’‘ To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.
Join Our Discord

from dspy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.