How can I retrieve the full OpenAI response (including top_logprobs) when calling Langchain's ChatOpenAI? #17634
-
Checked other resources
Commit to Help
Example Codefrom openai import OpenAI
statement = "I feel good today"
message = """
Return the a digit describing the sentiment of the following message:
"{statement}"
Return the digit 1 if the sentiment is positive, 0 if the sentiment is neutral, and 2 if the sentiment is negative.
Return just the one character digit, nothing else
""".format(
statement=statement
)
client = OpenAI()
m = client.models.list()
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=[
{"role": "user", "content": message},
],
logprobs=True,
top_logprobs=5,
)
print(response.choices[0].message.content)
print(response.choices[0].logprobs.content[0].top_logprobs) DescriptionThe above snippet shows how I can retrieve the log probability information from OpenAI models using their raw client. How can I achieve the same within LCEL? I can use System InfoSystem Information
Package Information
Packages not installed (Not Necessarily a Problem)The following packages were not found:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
🤖 Hey @EgorKraevTransferwise, great to see you back here! Diving into another interesting challenge, I see. Let's see what we can do about that. Based on the context provided, it seems you're looking to retrieve the full OpenAI response, including In LangChain, you can use the from langchain_community.llms.openai import ChatOpenAI
from langchain_community.llms.openai import HumanMessage
llm = ChatOpenAI()
result = llm.generate([[HumanMessage(content="I'm PickleRick")]], logprobs=True)
assert result.generations[0][0].generation_info
assert "content" in result.generations[0][0].generation_info["logprobs"] This code creates an instance of the The LangChain framework also provides a function to retrieve the full OpenAI response, including Here is the relevant code: def _stream_response_to_generation_chunk(
stream_response: Dict[str, Any],
) -> GenerationChunk:
"""Convert a stream response to a generation chunk."""
if not stream_response["choices"]:
return GenerationChunk(text="")
return GenerationChunk(
text=stream_response["choices"][0]["text"],
generation_info=dict(
finish_reason=stream_response["choices"][0].get("finish_reason", None),
logprobs=stream_response["choices"][0].get("logprobs", None),
),
) In this function, Please note that the I hope this helps! If you have any other questions, feel free to ask. Sources
|
Beta Was this translation helpful? Give feedback.
-
🤖 To integrate the retrieval of the full OpenAI response, including Currently, the Here is an example of how you could modify the class LLMChain(Chain):
...
def run(self, **inputs: Any) -> Dict[str, Any]:
...
response = self.llm.generate(prompt, **llm_kwargs)
return {
"generated_text": response["choices"][0]["text"]["content"],
"top_logprobs": response["choices"][0]["logprobs"]["top_logprobs"],
} Then, in your class ConversationalRetrievalChain(Chain):
...
def _call(self, inputs: Dict[str, Any], run_manager: Optional[CallbackManagerForChainRun] = None) -> Dict[str, Any]:
...
new_question = self.question_generator.run(
question=question, chat_history=chat_history_str, callbacks=callbacks
)
...
output[self.output_key] = answer
output["top_logprobs"] = new_question["top_logprobs"]
...
return output Please note that this is a simplified example and you may need to adjust it to fit your specific use case. Also, keep in mind that returning You can find more information about the Sources
|
Beta Was this translation helpful? Give feedback.
-
🤖 Based on your question, it seems you're looking to retrieve the full OpenAI response, including You're on the right track with the idea of using a RunnableLambda. The LangChain framework does indeed support the use of custom RunnableLambda classes. RunnableLambda is designed to convert a Python callable into a Runnable, making it usable within either a synchronous or asynchronous context. This class can be composed as any other Runnable and provides seamless integration with LangChain tracing. Here's an example of how you can create a custom RunnableLambda that wraps the from langchain_core.language_models.llms import LLMResult
from langchain_core.runnables import RunnableLambda
from langchain_community.llms.openai import ChatOpenAI
# Initialize the ChatOpenAI model
llm = ChatOpenAI(model_name="gpt-3.5-turbo-instruct")
# Create a RunnableLambda that wraps the llm.generate call
class CustomRunnableLambda(RunnableLambda):
async def run(self, input_: Any, **kwargs: Any) -> Any:
# Call the generate method of the language model
result: LLMResult = await llm.generate(input_, **kwargs)
# Extract the full OpenAI response
full_response = result.generation_info
return full_response
# Initialize the custom RunnableLambda
runnable = CustomRunnableLambda()
# Use the RunnableLambda to generate a response
response = await runnable.run("Hello, how are you?") In this example, the Please note that you need to replace You can find more information about the RunnableLambda class in the LangChain core library. I hope this helps! If you have any further questions, feel free to ask. Sources
|
Beta Was this translation helpful? Give feedback.
-
🤖 Sure, I can help you convert your asynchronous implementation to a synchronous one. Here's how you can do it: First, you need to replace the Here's how your updated from langchain_core.language_models.llms import LLMResult
from langchain_core.runnables import RunnableLambda
from langchain_community.llms.openai import ChatOpenAI
# Initialize the ChatOpenAI model
llm = ChatOpenAI(model_name="gpt-3.5-turbo-instruct")
# Create a RunnableLambda that wraps the llm.invoke call
class CustomRunnableLambda(RunnableLambda):
def run(self, input_: Any, **kwargs: Any) -> Any:
# Call the invoke method of the language model
result: LLMResult = llm.invoke(input_, **kwargs)
# Extract the full OpenAI response
full_response = result.generation_info
return full_response
# Initialize the custom RunnableLambda
runnable = CustomRunnableLambda()
# Use the RunnableLambda to generate a response
response = runnable.run("Hello, how are you?") In this updated code, the Please note that the I hope this helps! If you have any other questions, feel free to ask. Sources
|
Beta Was this translation helpful? Give feedback.
🤖
Hey @EgorKraevTransferwise, great to see you back here! Diving into another interesting challenge, I see. Let's see what we can do about that.
Based on the context provided, it seems you're looking to retrieve the full OpenAI response, including
top_logprobs
, when using theChatOpenAI
model within the LangChain framework.In LangChain, you can use the
logprobs=True
parameter when calling thegenerate
oragenerate
method. This will include the logprobs in thegeneration_info
of the response. Here's an example: