140 أسطر
4.1 KiB
Plaintext
140 أسطر
4.1 KiB
Plaintext
### **Ghaymah GenAI: Code Examples**
|
|
|
|
This section provides ready-to-use code snippets to help you get started with the Ghaymah GenAI API quickly. You can switch between different programming languages to find the example that best suits your project.
|
|
|
|
To begin, make sure you have your **API key** from the API Keys section.
|
|
|
|
#### **Python Example**
|
|
|
|
This example demonstrates how to make a chat completion request using Python with the `openai` library, which is compatible with our API.
|
|
|
|
Prerequisites:
|
|
|
|
Before running the code, you need to install the openai library. You can do this using pip:
|
|
```bash
|
|
pip install openai
|
|
```
|
|
**Code:**
|
|
|
|
|
|
|
|
```Python
|
|
from openai import OpenAI
|
|
|
|
# Initialize the client with your API key and the base URL
|
|
# It's a best practice to use environment variables for your API key.
|
|
# Replace 'YOUR_API_KEY' with your actual Ghaymah GenAI key.
|
|
client = OpenAI(
|
|
api_key="YOUR_API_KEY",
|
|
base_url="https://genai.ghaymah.systems"
|
|
)
|
|
|
|
# Make a request to the chat completions endpoint
|
|
response = client.chat.completions.create(
|
|
model="DeepSeek-V3-0324", ## Here You can choose the model you prefer
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": "Explain AI in simple terms"
|
|
}
|
|
],
|
|
max_tokens=100 #Max number of tokens your about should be.
|
|
)
|
|
|
|
# Print the content of the response from the model
|
|
print(response.choices[0].message.content)
|
|
|
|
```
|
|
|
|
**Code Breakdown:**
|
|
|
|
- `from openai import OpenAI`: Imports the necessary class to interact with the API.
|
|
|
|
- `client = OpenAI(...)`: Creates an instance of the client.
|
|
|
|
- `api_key`: Your unique API key for authentication.
|
|
|
|
- `base_url`: The base endpoint for all API requests.
|
|
|
|
- `client.chat.completions.create(...)`: This is the core API call.
|
|
|
|
- `model`: Specifies which AI model you want to use for the request. In this case, `DeepSeek-V3-0324`.
|
|
|
|
- `messages`: An array of message objects that form the conversation history. This example includes a single user message.
|
|
|
|
- `role`: The role of the speaker (`user`, `assistant`, or `system`).
|
|
|
|
- `content`: The text of the message.
|
|
|
|
- `max_tokens`: The maximum number of tokens the model is allowed to generate in its response.
|
|
|
|
- `print(...)`: Retrieves the text generated by the model and prints it to the console.
|
|
|
|
|
|
----------
|
|
|
|
#### **JavaScript Example**
|
|
|
|
This example demonstrates how to make a similar chat completion request using JavaScript.
|
|
|
|
Prerequisites:
|
|
|
|
You can use npm to install the openai package.
|
|
|
|
```bash
|
|
npm install openai
|
|
```
|
|
**Code:**
|
|
|
|
|
|
```JavaScript
|
|
|
|
import OpenAI from "openai";
|
|
|
|
// Initialize the client with your API key and the base URL
|
|
// It's a best practice to use environment variables for your API key.
|
|
// Replace 'YOUR_API_KEY' with your actual Ghaymah GenAI key.
|
|
const client = new OpenAI({
|
|
apiKey: "YOUR_API_KEY",
|
|
baseURL: "https://genai.ghaymah.systems"
|
|
});
|
|
|
|
async function main() {
|
|
try {
|
|
const response = await client.chat.completions.create({
|
|
model: "DeepSeek-V3-0324",
|
|
messages: [
|
|
{
|
|
"role": "user",
|
|
"content": "Explain AI in simple terms"
|
|
}
|
|
],
|
|
max_tokens: 100
|
|
});
|
|
|
|
console.log(response.choices[0].message.content);
|
|
} catch (error) {
|
|
console.error("Error making API call:", error);
|
|
}
|
|
}
|
|
|
|
main();
|
|
|
|
```
|
|
|
|
**Code Breakdown:**
|
|
|
|
- `import OpenAI from "openai";`: Imports the OpenAI client library.
|
|
|
|
- `const client = new OpenAI(...)`: Initializes the client with your API key and the custom base URL.
|
|
|
|
- `async function main()`: Defines an asynchronous function to handle the API call.
|
|
|
|
- `client.chat.completions.create(...)`: Makes the API call with the same parameters as the Python example (`model`, `messages`, `max_tokens`).
|
|
|
|
- `console.log(...)`: Logs the model's generated content to the console.
|
|
|
|
- `try...catch`: Includes basic error handling to catch and log any issues with the API request.
|
|
|
|
- `main()`: Calls the main function to execute the code.
|
|
|