dspy icon indicating copy to clipboard operation
dspy copied to clipboard

add support for multiple GPT3 clients

Open Su3h7aM opened this issue 1 year ago • 2 comments
trafficstars

In my tests, versions of openai above 1.0.0 are working correctly, however versions below 0.28.1 are crashing with the error:

choices = response["choices"]
               ~~~~~~~~^^^^^^^^^^^
KeyError: 'choices

but as this error also happens in main, I didn't look into what it could be. (I don't know how important it is for you to maintain compatibility with such an old version, but I'll take a look later anyway)


It was also necessary to make changes to databricks.py to adapt to the new behavior of the GPT3 class.


Pointing out that I did all the tests using the OpenAI-compatible API of llama.cpp, if anyone is willing to do tests with the OpenAI and Databricks APIs it would help to validate the PR. Another important fact is that this PR breaks compatibility with existing caches, I couldn't think of a way to keep the caches compatible (Taking into account how the cache was implemented, I don't think it's possible). The entire structure was based on azure_openai.py, I tried to make it consistent with other parts of the code.


Fix #915 / Alternative implementation to #744

Su3h7aM avatar Apr 28 '24 19:04 Su3h7aM

The behavior is the same using dspy.OpenAI or dspy.Databricks

Examples:

import dspy

llama3 = dspy.OpenAI(model="llama-3", api_base="http://localhost:8080/v1/", model_type="chat", api_key="no-key")
phi3 = dspy.OpenAI(model="phi-3", api_base="http://localhost:4040/v1/", model_type="chat", api_key="no-key")

print("llama3 client: ")
print(llama3._openai_client())
print("phi3 client: ")
print(phi3._openai_client())

dspy.settings.configure(lm=llama3)

question = "Who are you? Who developed you?"

class QA(dspy.Signature):
    question = dspy.InputField()
    answer = dspy.OutputField()

llama3_pred = dspy.Predict(QA)(question=question)

print("\nLlama3: ")
print(llama3_pred.answer)

with dspy.context(lm=phi3):
    phi3_pred = dspy.Predict(QA)(question=question)

    print("\nPhi3: ")
    print(phi3_pred.answer)

Main output:

llama3 client:
<module 'openai' from '/home/matheus/.local/share/mamba/envs/dspy/lib/python3.10/site-packages/openai/__init__.py'>
phi3 client:
<module 'openai' from '/home/matheus/.local/share/mamba/envs/dspy/lib/python3.10/site-packages/openai/__init__.py'>

Llama3:
Question: Who are you? Who developed you?
Answer: I am an AI digital assistant designed to provide information, answer questions, and assist with various tasks. I was developed by a team of engineers and researchers specializing in artificial intelligence and machine learning.<|end|>

Phi3:
Question: Who are you? Who developed you?
Answer: I am an AI digital assistant designed to provide information, answer questions, and assist with various tasks. I was developed by a team of engineers and researchers specializing in artificial intelligence and machine learning.<|end|>

PR output:

llama3 client:
<openai.OpenAI object at 0x7f813531af10>
phi3 client:
<openai.OpenAI object at 0x7f81011a2bd0>

Llama3:
Question: Who are you? Who developed you?
Answer: I am LLaMA, a large language model developed by Meta AI that was trained on a massive
 corpus of text from the internet.

Phi3:
Question: Who are you? Who developed you?
Answer: I am an AI digital assistant designed to provide information, answer questions, and assist with various tasks. I was developed by a team of engineers and researchers specializing in artificial intelligence and machine learning.<|end|>

Su3h7aM avatar Apr 28 '24 20:04 Su3h7aM

Hi @Su3h7aM , thanks for raising this PR! These changes are similar to #744 as you've mentioned, and in both cases, this breaks existing caches, so we can't merge the PR. Tagging the comment from there:

yeah ideally if we can add this support without modifying the existing behavior, this won't break the existing caches. Curious if you can only add the client creation as a separate flag, while keeping the rest of the cache function behavior the same.

Also note that Databricks Model Serving is not compatible with openai 0.28.1 so the changes to dspy.Databricks can be reverted.

arnavsinghvi11 avatar May 06 '24 00:05 arnavsinghvi11