Skip to content

Add cache to async LM call #8135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 30, 2025
Merged

Conversation

chenmoneygithub
Copy link
Collaborator

@chenmoneygithub chenmoneygithub commented Apr 29, 2025

Support DSPy cache on the async LM call, and also simplify the code to avoid the redundant methods like cached_litellm_completion.

Sample testing script:

import asyncio
import time

import dspy

dspy.configure(lm=dspy.LM("openai/gpt-4o-mini", cache=True))

cot = dspy.ChainOfThought("question -> answer")


async def test_cot():
    start_time = time.time()
    result = await cot.acall(question="What is main character of Demon Slayer?")
    print(result)
    end_time = time.time()
    print(f"Time taken for the first async call: {end_time - start_time} seconds")

    start_time = time.time()
    result = await cot.acall(question="What is main character of Demon Slayer?")
    print(result)
    end_time = time.time()
    print(f"Time taken for the second async call: {end_time - start_time} seconds")


asyncio.run(test_cot())

start_time = time.time()
result = cot(question="What is main character of Tom and Jerry?")
print(result)
end_time = time.time()
print(f"Time taken for the first sync call: {end_time - start_time} seconds")

start_time = time.time()
result = cot(question="What is main character of Tom and Jerry?")
print(result)
end_time = time.time()
print(f"Time taken for the second sync call: {end_time - start_time} seconds")

output:

Prediction(
    reasoning='The main character of "Demon Slayer: Kimetsu no Yaiba" is Tanjiro Kamado. He is a young boy who becomes a demon slayer after his family is slaughtered by demons, and his younger sister Nezuko is turned into a demon. Tanjiro is characterized by his strong sense of justice, compassion, and determination to save his sister and avenge his family.',
    answer='The main character of Demon Slayer is Tanjiro Kamado.'
)
Time taken for the first async call: 2.550046920776367 seconds
Prediction(
    reasoning='The main character of "Demon Slayer: Kimetsu no Yaiba" is Tanjiro Kamado. He is a young boy who becomes a demon slayer after his family is slaughtered by demons, and his younger sister Nezuko is turned into a demon. Tanjiro is characterized by his strong sense of justice, compassion, and determination to save his sister and avenge his family.',
    answer='The main character of Demon Slayer is Tanjiro Kamado.'
)
Time taken for the second async call: 0.0007500648498535156 seconds
Prediction(
    reasoning='The main characters of "Tom and Jerry" are Tom, a cat, and Jerry, a mouse. The series revolves around the comedic rivalry between the two, with Tom often trying to catch Jerry, who always manages to outsmart him. Their interactions are central to the show\'s humor and appeal.',
    answer='The main characters of "Tom and Jerry" are Tom the cat and Jerry the mouse.'
)
Time taken for the first sync call: 2.3556289672851562 seconds
Prediction(
    reasoning='The main characters of "Tom and Jerry" are Tom, a cat, and Jerry, a mouse. The series revolves around the comedic rivalry between the two, with Tom often trying to catch Jerry, who always manages to outsmart him. Their interactions are central to the show\'s humor and appeal.',
    answer='The main characters of "Tom and Jerry" are Tom the cat and Jerry the mouse.'
)
Time taken for the second sync call: 0.0008280277252197266 seconds

@chenmoneygithub chenmoneygithub requested a review from okhat April 29, 2025 02:56
@okhat okhat merged commit 576f33b into stanfordnlp:main Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants