Function Calling with GPT
Function Calling with GPT
Connect Desearch to OpenAI models through tool calling, so GPT can run live search queries when it needs fresh data.
Function Calling with GPT
1. Installation and Requirements
PYTHON
- openai: OpenAI API client
- desearch_py: Desearch search
- rich: terminal output
2. Setting Environment Variables
Create a .env file in the root of your project:
Get your API keys from:
- <Anchor label="OpenAI API Key" target="_blank" href="https://platform.openai.com/api-keys">OpenAI API Key</Anchor>
- <Anchor label="Desearch API Key" target="_blank" href="https://console.desearch.ai/api-keys">Desearch API Key</Anchor>
3. What Is Tool Calling?
Tool calling lets OpenAI models return structured calls to functions you define. A sample schema:
JSON
OpenAI returns a function name and its parameters. Your code executes the actual call.
4. Integrating Desearch as an OpenAI Tool
Step 1: Initialize Your Clients
PYTHON
Step 2: Define Tool Schema
PYTHON
Step 3: Define the System Prompt:
PYTHON
Step 4: Create the Desearch Function
PYTHON
Step 5: Handle Tool Calls
PYTHON
Step 6: Build the Main Loop
PYTHON
5. Run the App
Save the code as openai_search.py with your .env file in the same directory. Run it:
PYTHON
You'll be prompted:
Type a query and the assistant answers using Desearch results:
What do you want to search for?: What is bittensor?
Context updated with desearch_search (None): What is bittensor?
Bittensor is a decentralized AI network designed to create a marketplace for machine learning models and AI training services. Unlike traditional AI systems, which are often restricted by centralized APIs or paywalls, Bittensor utilizes blockchain technology to allow open and permissionless participation in AI development. The primary aim is to democratize access to AI and build a collaborative ecosystem where contributors earn rewards for training and enhancing AI models.
At its core, Bittensor functions as a decentralized marketplace where participants, referred to as miners, contribute computational resources to train machine learning models. In return, these miners earn TAO tokens as rewards. This structure fosters a competitive market based on incentives, encouraging ongoing innovation and improvement of AI technologies.
Bittensor is characterized as a network of incentive-driven subnets rather than a traditional blockchain like Bitcoin or Ethereum. Each subnet acts as a competitive market for AI training, promoting a self-sustaining economy wherein AI models are continuously refined through collective contributions.
The ecosystem aims to integrate AI with Web3 principles, emphasizing ownership, control, and open participation. By facilitating a decentralized AI marketplace, Bittensor seeks to drive innovation through collaborative effort and financial incentives, potentially leading to more diverse and powerful AI models.
In summary, Bittensor represents an innovative approach to merging blockchain technology with AI training and marketplace dynamics, promoting a shift towards decentralized AI ownership and collaboration.
Full code
PYTHON