Use a Mind via OpenAI Assistants API

Once the Mind is created, you can chat with the Mind using the standard OpenAI Assistants API. Therefore to follow this code, if you haven’t already, you need to install the OpenAI SDK by running pip install openai, and use the Minds API key and name of the Mind you created.

The following code lets you have conversations with the Mind, create threads, messages, and runs.

from openai import OpenAI

your_minds_api_key = <YOUR API KEY>
mind_name = <NAME YOU GAVE TO YOUR MIND>

# point the Openai SDK to the Minds Cloud
client = OpenAI(
    api_key=your_minds_api_key,
    base_url='https://llm.mdb.ai/'
)

# print the message before making the API request
print('Answering the question may take up to 30 seconds...')

thread = client.beta.threads.create()

message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="How many 2-bedroom houses are on the market?"
)

# run the message on the mind that you created
run = client.beta.threads.runs.create_and_poll(
    thread_id=thread.id,
    assistant_id=mind_name
)

if run.status == 'completed':
    messages = client.beta.threads.messages.list(
      thread_id=thread.id
    )
    print(messages.data[1].role + ': ' + messages.data[1].content[0].text.value)
    print(messages.data[0].role + ': ' + messages.data[0].content[0].text.value)
else:
    print(run.status)

client.beta.threads.delete(thread.id)

Here is the output:

Answering the question may take up to 30 seconds...
user: How many 2-bedroom houses are on the market?
assistant: There are 99 two-bedroom houses that were once on the market according to the data in the database.

Quickstart

Execute the code right away to see Minds in action!

  1. Sign up at Minds Cloud and generate your Minds API key.
  2. Open this Google Collab notebook that contains ready-to-run code.
  3. In the Google Colab notebook, add a secret named minds_api_key with the value being your Minds API key.
Google Colab - Add Secret
  1. Run the codes.

Was this page helpful?