Skip to content

Commit 1cde82b

Browse files
committed
fix: assistent
1 parent e10ab40 commit 1cde82b

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

openai_assistant.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ def get_assistant_by_name(self, name):
1818
raise OpenAIAssistantError(f"An error occurred while retrieving assistants: {e}")
1919

2020
def get_assistant(self, assistant_id):
21-
# Retrieve the assistant details by ID from the OpenAI API
2221
try:
23-
assistant = self.client.beta.assistants.retrieve(assistant_id)
24-
return assistant
22+
return self.client.beta.assistants.retrieve(assistant_id)
2523
except Exception as e:
26-
print(f"An error occurred while retrieving the assistant: {e}")
2724
raise OpenAIAssistantError(f"An error occurred while retrieving the assistant: {e}")
2825

2926
def create_assistant(self, name, model, description=None, tools=None, file_ids=None):
@@ -56,18 +53,21 @@ def list_assistants(self):
5653
except Exception as e:
5754
raise OpenAIAssistantError(f"An error occurred while listing assistants: {e}")
5855

59-
def create_thread(self, assistant_id, initial_message, file_ids=None):
56+
# Adjusted the method signature to match expected OpenAI API behavior
57+
def create_thread(self, assistant_id, initial_message, file_id=None):
6058
try:
61-
return self.client.beta.threads.create(
59+
# Assuming that creating a thread is supported by the OpenAI API
60+
thread = self.client.beta.threads.create(
6261
assistant_id=assistant_id,
6362
messages=[
6463
{
6564
"role": "user",
6665
"content": initial_message,
67-
"file_ids": file_ids or []
66+
"file_ids": [file_id] if file_id else []
6867
}
6968
]
7069
)
70+
return thread
7171
except Exception as e:
7272
raise OpenAIAssistantError(f"An error occurred while creating a thread: {e}")
7373

@@ -83,18 +83,16 @@ def list_threads(self, assistant_id):
8383
except Exception as e:
8484
raise OpenAIAssistantError(f"An error occurred while listing threads: {e}")
8585

86-
def chat_with_assistant(self, thread_id, message, file_ids=None):
86+
# Adjusted the method signature to match expected OpenAI API behavior
87+
def chat_with_assistant(self, thread_id, assistant_id, message):
8788
try:
88-
return self.client.beta.threads.messages.create(
89+
# Assuming that chatting with an assistant is supported by the OpenAI API
90+
response = self.client.beta.threads.runs.create(
8991
thread_id=thread_id,
90-
messages=[
91-
{
92-
"role": "user",
93-
"content": message,
94-
"file_ids": file_ids or []
95-
}
96-
]
92+
assistant_id=assistant_id,
93+
messages=[{"role": "user", "content": message}]
9794
)
95+
return response
9896
except Exception as e:
9997
raise OpenAIAssistantError(f"An error occurred while chatting with the assistant: {e}")
10098

@@ -110,8 +108,3 @@ def handle_command(self, command, thread_id=None, assistant_id=None):
110108
return "Invalid command."
111109
except OpenAIAssistantError as e:
112110
return str(e)
113-
114-
# Example usage to test:
115-
# client = openai.Client(api_key='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')
116-
# assistant = OpenAIAssistant(client)
117-
# assistant.create_or_get_assistant('MyAssistant', 'gpt-4-1106-preview')

0 commit comments

Comments
 (0)