#AI #textgrad

In this blog, I’ll show you how to use models like DeepSeek with TextGrad. Initially, I thought TextGrad couldn’t work with any model other than OpenAI’s, but after checking the examples and code in the official GitHub, I realized I was mistaken.You can check this official example to learn more..

I chose a question from the June 25, 2024 TextGrad example used in the official documentation. The question is as follows:

If it takes 1 hour to dry 25 shirts under the sun, how long will it take to dry 30 shirts under the sun?

So, the code used DeepSeek to answer this question is shown as follows

from openai import OpenAI
from textgrad.engine.local_model_openai_api import ChatExternalClient
import textgrad as tg

client = OpenAI(base_url="https://api.deepseek.com", api_key="<deepseek api key>")
engine = ChatExternalClient(client=client, model_string='deepseek-chat')
tg.set_backward_engine(engine, override=True)


# Step 1: Get an initial response from an LLM.
model = tg.BlackboxLLM(engine)
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)
print("first answer:",answer)
print('-'*50)

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

# Step 2: Define the loss function and the optimizer, just like in PyTorch!
# Here, we don't have SGD, but we have TGD (Textual Gradient Descent)
# that works with "textual gradients".

optimizer = tg.TGD(parameters=[answer])
evaluation_instruction = (f"Here's a question: {question_string}. "
"Evaluate any given answer to this question, "
"be very smart, logical, careful, and critical. "
"Just identify the error in the answer and provide concise feedback.")

# TextLoss is a natural-language specified loss function that describes
# how we want to evaluate the reasoning.
loss_fn = tg.TextLoss(evaluation_instruction)

# Step 3: Do the loss computation, backward pass, and update the punchline.
# Exact same syntax as PyTorch!

epoch = 5

for _ in range(epoch):
loss = loss_fn(answer)
loss.backward()
optimizer.step()
print(answer.value)
print('-'*50)
print('final answer:',answer.value)

The outputs are as follows

first answer: To determine how long it will take to dry 30 shirts under the sun, we can use the information provided about the drying rate of shirts. Here's the step-by-step reasoning:

1. **Determine the drying rate per shirt:**
- It takes 1 hour to dry 25 shirts.
- Therefore, the drying rate is 25 shirts per hour.

2. **Calculate the time required for 30 shirts:**
- Since the drying rate is 25 shirts per hour, we need to find out how many hours it will take to dry 30 shirts.
- We can set up a proportion to solve this:
\[
\text{Time} = \frac{\text{Number of shirts}}{\text{Drying rate}}
\]
\[
\text{Time} = \frac{30 \text{ shirts}}{25 \text{ shirts/hour}}
\]
\[
\text{Time} = 1.2 \text{ hours}
\]

3. **Interpret the result:**
- It will take 1.2 hours to dry 30 shirts under the sun.

So, the answer is that it will take 1.2 hours to dry 30 shirts under the sun.
--------------------------------------------------
It will take 1.2 hours to dry 30 shirts under the sun.
It will take approximately 1.2 hours to dry 30 shirts under the sun, considering a linear scaling based on the drying rate of 25 shirts per hour.
It may take slightly more than 1 hour to dry 30 shirts under the sun, but this could vary depending on factors such as space and sunlight exposure.
It may take slightly more than 1 hour to dry 30 shirts under the sun, but this could vary significantly depending on factors such as space and sunlight exposure, which might lead to a non-linear increase in drying time.
It may take approximately 1.2 hours to dry 30 shirts under the sun, considering a slight decrease in drying efficiency due to increased crowding and reduced sunlight exposure per shirt.
--------------------------------------------------
final answer: It may take approximately 1.2 hours to dry 30 shirts under the sun, considering a slight decrease in drying efficiency due to increased crowding and reduced sunlight exposure per shirt.

Obviously, the final answer is still not completely correct. However, it does show some improvements over the original answer. The model starts considering various conditions to answer the question. Increasing the epoch number might help in pursuing the correct answer. It’s possible that even when the gradient converges, the model may not always get the right answer. TextGrad is an optimization framework, and the underlying model is also crucial in determining the final answer.

In this blog, I demonstrated how to use another model with TextGrad using a tricky question as an example. If you want to use models like DeepSeek, Qwen2, and others, you can follow the steps outlined here.