Closed ADianan closed 10 months ago
🤖
Hello @ADianan,
It's a pleasure to meet you! I'm Dosu, an AI assistant here to help you with LangChain. I can assist in solving bugs, answering questions, and guide you on your journey to becoming a contributor. While we wait for a human maintainer, feel free to ask me anything. Let's make your experience with LangChain as smooth as possible!
It seems like you've got this under control, if you want help or have specific questions, let me know what I can do for you!
how do i use existing openai assistant instead of creating new ones with OpenaiRunnable
From: dosubot[bot] @.> Sent: 16 November 2023 6:40 PM To: langchain-ai/langchain @.> Cc: Adenan Hasan Shaleh @.>; Mention @.> Subject: Re: [langchain-ai/langchain] OpenAI Assitant (Issue #13453)
🤖
Hello @ADiananhttps://github.com/ADianan,
It's a pleasure to meet you! I'm Dosu, an AI assistant here to help you with LangChain. I can assist in solving bugs, answering questions, and guide you on your journey to becoming a contributor. While we wait for a human maintainer, feel free to ask me anything. Let's make your experience with LangChain as smooth as possible!
It seems like you've got this under control, if you want help or have specific questions, let me know what I can do for you!
— Reply to this email directly, view it on GitHubhttps://github.com/langchain-ai/langchain/issues/13453#issuecomment-1814191091, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AFRJLSJRG2MI7YNJL4CCWULYEXUTJAVCNFSM6AAAAAA7N5VVLWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJUGE4TCMBZGE. You are receiving this because you were mentioned.Message ID: @.***>
🤖
Based on your request, you would like to modify the LangChain Python framework to use an existing OpenAI assistant by providing the assistant ID in the Constructor for OpenAIRunnable. Currently, the create_assistant
method in the LangChain Python framework does not support this functionality. It creates a new OpenAI assistant every time it is called.
However, you can modify the create_assistant
method to accept an optional assistant_id
parameter. If an assistant_id
is provided, it will use that assistant instead of creating a new one. Here's how you can modify the create_assistant
method:
@classmethod
def create_assistant(
cls,
name: str,
instructions: str,
tools: Sequence[Union[BaseTool, dict]],
model: str,
*,
client: Optional[openai.OpenAI] = None,
assistant_id: Optional[str] = None, # Add this line
**kwargs: Any,
) -> OpenAIAssistantRunnable:
"""Create an OpenAI Assistant and instantiate the Runnable.
Args:
name: Assistant name.
instructions: Assistant instructions.
tools: Assistant tools. Can be passed in in OpenAI format or as BaseTools.
model: Assistant model to use.
client: OpenAI client. Will create default client if not specified.
assistant_id: OpenAI assistant id. Will create a new assistant if not specified. # Add this line
Returns:
OpenAIAssistantRunnable configured to run using the created assistant.
"""
client = client or _get_openai_client()
openai_tools: List = []
for tool in tools:
oai_tool = (
tool if isinstance(tool, dict) else format_tool_to_openai_tool(tool)
)
openai_tools.append(oai_tool)
if assistant_id is None: # Add this line
assistant = client.beta.assistants.create(
name=name,
instructions=instructions,
tools=openai_tools,
model=model,
)
assistant_id = assistant.id # Add this line
return cls(assistant_id=assistant_id, **kwargs) # Modify this line
This modification allows you to create an OpenAIAssistantRunnable
instance with an existing assistant by providing the assistant_id
when calling create_assistant
. If assistant_id
is not provided, it will create a new assistant as before.
Please note that this modification should be done in the base.py
file located at libs/langchain/langchain/agents/openai_assistant/base.py
in the LangChain repository.
I hope this helps! If you have any other questions or need further clarification, feel free to ask.
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
@ADianan The code for OpenAI Assistants have already been merged into langchain master as part of Agents, just pending the next ad-hoc PyPI release, or using the master branch directly rather than a release.
@Jyumpp that is not what this is about. The AI answer seems to be correct: there is currently no easy way to reuse assistants with langchain. This results in langchain agents recreating agents every single run und thus cluttering the assistant list within OpenAI
Edit: You can actually use custom assistants now in this way:
interpreter_assistant = OpenAIAssistantRunnable(
assistant_id="asst_gX2OQyi6qgxXr7NbaJeZ6w17"
)
But there is an error when trying to specify a custom client (related to pydantic)
interpreter_assistant = OpenAIAssistantRunnable(
assistant_id="asst_gX2OQyi6qgxXr7NbaJeZ6w17", client=openai.OpenAI()
)
As @Pflyg pointed out, you just specify the assistant ID. (doc link: :https://python.langchain.com/docs/modules/agents/agent_types/openai_assistants#using-existing-assistant)
I want to create an OpenAI assistant using an existing ID, but I'm encountering an error.
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable
pydantic.v1.errors.ConfigError: field "client" not yet prepared, so the type is still a ForwardRef. You might need to call OpenAIAssistantRunnable.update_forward_refs()
In both cases, an error is encountered. The only successful scenario is when the openai_api_key is set in the environment, but my requirement is for the user to provide it. Has anyone else faced a similar issue? Any assistance would be appreciated.
@hinthornw @Pflyg @ADianan
How to modified or delete with OpenAIAssistantRunnable
?
I think we need to add code similar to the following.
@classmethod
def delete_assistant(
assistant_id: str
client: Optional[Union[openai.OpenAI, openai.AzureOpenAI]] = None
):
client = client or _get_openai_client()
return client.beta.assistants.delete()
Hi @hasansustcse13 , you can include the parameter client= OpenAI(api_key=OPENAI_APIKEY) to specify the use of the passed API KEY to create the agent. The complete code would be as follows:
agent = OpenAIAssistantRunnable(client=OpenAI(api_key=openai_api_key), assistant_id=assistant_id, as_agent=True)
How to modified or delete with
OpenAIAssistantRunnable
?I think we need to add code similar to the following.
@classmethod def delete_assistant( assistant_id: str client: Optional[Union[openai.OpenAI, openai.AzureOpenAI]] = None ): client = client or _get_openai_client() return client.beta.assistants.delete()
I face the same issue, hope langchain can support delete assistant
Feature request
Can you allow the usage of existing Open ai assistant instead of creating a new one every time when using OpenAI Runnable
Motivation
I dont want to clutter my assistant list with a bunch of clones
Your contribution
The developer should only provide the assistant ID in the Constructor for OpenAIRunnable