> ## Documentation Index
> Fetch the complete documentation index at: https://composio-27-feat-docs-revamp.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ֎ Using Composio With OpenAI

> Integrate Composio with OpenAI Assistants to let them seamlessly interact with external apps

## Star A Repository on Github

In this example, we will use OpenAI Assistant to star a repository on Github using Composio Tools

<Steps>
  <Step title="Install Packages">
    <CodeGroup>
      ```bash Python
      pip install composio-openai openai
      ```

      ```javascript JavaScript
      npm i composio-core openai
      ```
    </CodeGroup>
  </Step>

  <Step title="Import Libraries & Initialize ComposioToolSet & LLM">
    <CodeGroup>
      ```python Python
      from openai import OpenAI
      from composio_openai import ComposioToolSet, Action

      openai_client = OpenAI()
      composio_toolset = ComposioToolSet()
      ```

      ```javascript JavaScript
      import { OpenAIToolSet } from "composio-core";
      import OpenAI from "openai";

      const toolset = new OpenAIToolSet();
      const openai = new OpenAI();
      ```
    </CodeGroup>
  </Step>

  <Step title="Connect Your GitHub Account">
    <Info>You need to have an active GitHub Integration. Learn how to do this [here](https://youtu.be/LmyWy4LiedQ?si=u5uFArlNL0tew0Wf)</Info>

    <CodeGroup>
      ```shell CLI
      composio login
      composio add github
      ```

      ```python Python
      request = composio_toolset.initiate_connection(app=App.GITHUB)
      print(f"Open this URL to authenticate: {request.redirectUrl}")
      ```

      ```javascript JavaScript
      const connection = await toolset.connectedAccounts.initiate({appName: "github"})
      console.log(`Open this URL to authenticate: ${connection.redirectUrl}`);
      ```
    </CodeGroup>

    <Tip>
      Don't forget to set your `COMPOSIO_API_KEY` and `OPENAI_API_KEY` in your environment variables.
    </Tip>
  </Step>

  <Step title="Get All Github Tools">
    You can get all the tools for a given app as shown below, but you can get **specific actions** and filter actions using **usecase** & **tags**. Learn more [here](../../patterns/tools/use-tools/use-specific-actions)

    <CodeGroup>
      ```python Python
      tools = composio_toolset.get_tools(apps=[App.GITHUB])
      ```

      ```javascript JavaScript
      const tools = await toolset.getTools({ apps: ["github"] });
      ```
    </CodeGroup>
  </Step>

  <Step title="Define the Assistant">
    <CodeGroup>
      ```python Python
      assistant_instruction = "You are a super intelligent personal assistant"

      assistant = openai_client.beta.assistants.create(
        name="Personal Assistant",
        instructions=assistant_instruction,
        model="gpt-4-turbo-preview",
        tools=tools,
      )

      thread = openai_client.beta.threads.create()
      my_task = "Star a repo composiohq/composio on GitHub"
      message = openai_client.beta.threads.messages.create(thread_id=thread.id,role="user",content=my_task)

      run = openai_client.beta.threads.runs.create(thread_id=thread.id,assistant_id=assistant.id)

      response_after_tool_calls = composio_toolset.wait_and_handle_assistant_tool_calls(
          client=openai_client,
          run=run,
          thread=thread,
      )
      ```

      ```javascript JavaScript
      async function createGithubAssistant(openai, tools) {
          return await openai.beta.assistants.create({
              name: "Github Assistant",
              instructions: "You're a GitHub Assistant, you can do operations on GitHub",
              tools: tools,
              model: "gpt-4o-mini"
          });
      }
      ```
    </CodeGroup>
  </Step>

  <Step title="Execute the Agent">
    <CodeGroup>
      ```python Python
      print(response_after_tool_calls)
      ```

      ```javascript JavaScript
      //With Streaming
      async function executeAssistantTask(openai, toolset, assistant, task) {
          const thread = await openai.beta.threads.create();
          const run = await openai.beta.threads.runs.create(thread.id, {
              assistant_id: assistant.id,
              instructions: task,
              tools: tools,
              model: "gpt-4o-mini",
              stream: true
          });

          for await (const result of toolset.waitAndHandleAssistantStreamToolCalls(openai, run, thread)) {
              console.log(result);
          }
      }

      // Without Streaming
      async function executeAssistantTask(openai, toolset, assistant, task) {
          const thread = await openai.beta.threads.create();
          const run = await openai.beta.threads.runs.create(thread.id, {
              assistant_id: assistant.id,
              instructions: task,
              tools: tools,
              model: "gpt-4o-mini",
              stream: false
          });
          const call = await toolset.waitAndHandleAssistantToolCalls(openai, run, thread);
          console.log(call);
      }

      (async() => {
          const githubAssistant = await createGithubAssistant(openai, tools);
          await executeAssistantTask(
              openai, 
              toolset, 
              githubAssistant, 
              "Star the repository 'composiohq/composio'"
          );
      })();
      ```
    </CodeGroup>
  </Step>
</Steps>
