Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
vinid committed Jun 12, 2024
1 parent 28a7aae commit 1313d38
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,46 @@ This API is similar to the Pytorch API, making it simple to adapt to your usecas

## QuickStart
If you know PyTorch, you know 80% of TextGrad.
Let's walk through the key components with a simple example. Say we want to use GPT-4o to improve a solution to a math problem using TextGrad.
Let's walk through the key components with a simple example. Say we want to use GPT-4o to solve a simple
reasoning problem.

The question is "If it takes 1 hour to dry 25 shirts under the sun, how long will it take to dry 30 shirts under the sun? Reason step by step."
If you think about it **the answer is 1 hour**, since the number of shirts doesn't affect the time it takes to dry them.

```python
import textgrad as tg
# Step 1: Get an initial response from an LLM.

tg.set_backward_engine("gpt-4o", override=True)

model = tg.BlackboxLLM("gpt-4o")
punchline = model(tg.Variable("write a punchline for my github package about optimizing compound AI systems", role_description="prompt", requires_grad=False))
punchline.set_role_description("a concise punchline that must hook everyone")
question_string = "If it takes 1 hour to dry 25 shirts under the sun, how long will it take to dry 30 shirts under the sun? Reason step by step"
question = tg.Variable(question_string, role_description="question to the LLM", requires_grad=False)

answer = model(question)
# answer: To determine how long it will take to dry 30 shirts under the sun,
# we can use a proportional relationship based on the given information.
# Here’s the step-by-step reasoning: [.....]
# So, it will take 1.2 hours (or 1 hour and 12 minutes) to dry 30 shirts under the sun.
```

```python

answer.set_role_description("concise and accurate answer to the question")

optimizer = tg.TGD(parameters=[answer])
evaluation_instruction = f"Here's a question: {question_string}. Evaluate any given answer to this question, be smart, logical, and very critical. Just provide concise feedback."
loss_fn = tg.TextLoss(evaluation_instruction)
loss = loss_fn(answer)
# loss: [...] Your step-by-step reasoning is clear and logical,
# but it contains a critical flaw in the assumption that drying time is directly proportional
# to the number of shirts. [..]

loss.backward()
optimizer.step()
# answer: It will still take 1 hour to dry 30 shirts under the sun,
# assuming they are all laid out properly to receive equal sunlight.


```

Initial `punchline` from the model:
Expand Down

0 comments on commit 1313d38

Please sign in to comment.