text
stringlengths 0
1.15k
|
|---|
client = openai.OpenAI(base_url="https://YOUR-NODE-ID.gaia.domains/v1", api_key="YOUR_API_KEY_GOES_HERE")
|
```
|
Alternatively, you could set an environment variables at the OS level.
|
```
|
export OPENAI_API_BASE=https://YOUR-NODE-ID.gaia.domains/v1
|
export OPENAI_API_KEY=YOUR_API_KEY_GOES_HERE
|
```
|
Then, when you make API calls from the `client`, make sure that the `model` is set to the model name
|
available on your node.
|
```py
|
response = client.chat.completions.create(
|
model="Meta-Llama-3-8B-Instruct-Q5_K_M",
|
messages=[
|
{"role": "system", "content": "You are a strategic reasoner."},
|
{"role": "user", "content": "What is the purpose of life?"}
|
],
|
temperature=0.7,
|
max_tokens=500
|
]
|
)
|
```
|
That's it! You can now take any application built with the official OpenAI Python library and use a Gaia node
|
as its backend!
|
## The OpenAI Node API library
|
:::note
|
Make sure to replace `YOUR_API_KEY_GOES_HERE` with your **own API key**. To get your own API key, follow [this](../getting-started/authentication) tutorial.
|
:::
|
You can install the [OpenAI Node library](https://www.npmjs.com/package/openai) which provides convenient access to the OpenAI REST API from TypeScript or JavaScript as follows:
|
```
|
npm install openai
|
```
|
Import it into your project as:
|
```js
|
// Example usage in Node.js
|
const OpenAI = require('openai');
|
```
|
Create an OpenAI client with a custom base URL. Remember to append the `/v1` after the host name.
|
```js
|
const client = new OpenAI({
|
baseURL: 'https://YOUR-NODE-ID.gaia.domains/v1',
|
apiKey: 'YOUR_API_KEY_GOES_HERE'
|
});
|
```
|
Alternatively, you can set an environment variable using `dotenv` in Node.
|
```
|
process.env.OPENAI_API_BASE = 'https://YOUR-NODE-ID.gaia.domains/v1';
|
```
|
Then, when you make API calls from the `client`, make sure that the `model` is set to the model name
|
available on your node.
|
```js
|
async function callOpenAI() {
|
try {
|
const response = await client.chat.completions.create({
|
model: "Meta-Llama-3-8B-Instruct-Q5_K_M",
|
messages: [
|
{ role: "system", content: "You are a strategic reasoner." },
|
{ role: "user", content: "What is the purpose of life?" }
|
],
|
temperature: 0.7,
|
max_tokens: 500
|
});
|
console.log(response.choices[0].message.content);
|
} catch (error) {
|
console.error('Error:', error);
|
}
|
}
|
//Usage
|
callOpenAI();
|
```
|
---
|
## Langchain Integration
|
import Tabs from '@theme/Tabs';
|
import TabItem from '@theme/TabItem';
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.