diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2024-10-30 09:57:55 +0100 |
---|---|---|
committer | Tekky <98614666+xtekky@users.noreply.github.com> | 2024-10-30 09:57:55 +0100 |
commit | 1443c60cc86f7f02cc6c7a4b2a31d6b1dad66a26 (patch) | |
tree | 435d831df4ad5c18839cfaa647f23e5be035cdd6 /docs/interference-api.md | |
parent | implement direct import of `Client` without using `g4f.client` (diff) | |
parent | Merge pull request #2304 from kqlio67/main (diff) | |
download | gpt4free-1443c60cc86f7f02cc6c7a4b2a31d6b1dad66a26.tar gpt4free-1443c60cc86f7f02cc6c7a4b2a31d6b1dad66a26.tar.gz gpt4free-1443c60cc86f7f02cc6c7a4b2a31d6b1dad66a26.tar.bz2 gpt4free-1443c60cc86f7f02cc6c7a4b2a31d6b1dad66a26.tar.lz gpt4free-1443c60cc86f7f02cc6c7a4b2a31d6b1dad66a26.tar.xz gpt4free-1443c60cc86f7f02cc6c7a4b2a31d6b1dad66a26.tar.zst gpt4free-1443c60cc86f7f02cc6c7a4b2a31d6b1dad66a26.zip |
Diffstat (limited to 'docs/interference-api.md')
-rw-r--r-- | docs/interference-api.md | 106 |
1 files changed, 79 insertions, 27 deletions
diff --git a/docs/interference-api.md b/docs/interference-api.md index 4050f84f..2e18e7b5 100644 --- a/docs/interference-api.md +++ b/docs/interference-api.md @@ -1,23 +1,30 @@ - # G4F - Interference API Usage Guide - + ## Table of Contents - [Introduction](#introduction) - [Running the Interference API](#running-the-interference-api) - [From PyPI Package](#from-pypi-package) - [From Repository](#from-repository) - - [Usage with OpenAI Library](#usage-with-openai-library) - - [Usage with Requests Library](#usage-with-requests-library) + - [Using the Interference API](#using-the-interference-api) + - [Basic Usage](#basic-usage) + - [With OpenAI Library](#with-openai-library) + - [With Requests Library](#with-requests-library) - [Key Points](#key-points) + - [Conclusion](#conclusion) + ## Introduction -The Interference API allows you to serve other OpenAI integrations with G4F. It acts as a proxy, translating requests to the OpenAI API into requests to the G4F providers. +The G4F Interference API is a powerful tool that allows you to serve other OpenAI integrations using G4F (Gpt4free). It acts as a proxy, translating requests intended for the OpenAI API into requests compatible with G4F providers. This guide will walk you through the process of setting up, running, and using the Interference API effectively. + ## Running the Interference API +**You can run the Interference API in two ways:** using the PyPI package or from the repository. + ### From PyPI Package -**You can run the Interference API directly from the G4F PyPI package:** +**To run the Interference API directly from the G4F PyPI package, use the following Python code:** + ```python from g4f.api import run_api @@ -25,37 +32,80 @@ run_api() ``` - ### From Repository -Alternatively, you can run the Interference API from the cloned repository. +**If you prefer to run the Interference API from the cloned repository, you have two options:** -**Run the server with:** +1. **Using the command line:** ```bash g4f api ``` -or + +2. **Using Python:** ```bash python -m g4f.api.run ``` +**Once running, the API will be accessible at:** `http://localhost:1337/v1` -## Usage with OpenAI Library +## Using the Interference API - +### Basic Usage +**You can interact with the Interference API using curl commands for both text and image generation:** +**For text generation:** +```bash +curl -X POST "http://localhost:1337/v1/chat/completions" \ + -H "Content-Type: application/json" \ + -d '{ + "messages": [ + { + "role": "user", + "content": "Hello" + } + ], + "model": "gpt-3.5-turbo" + }' +``` + +**For image generation:** +1. **url:** +```bash +curl -X POST "http://localhost:1337/v1/images/generate" \ + -H "Content-Type: application/json" \ + -d '{ + "prompt": "a white siamese cat", + "model": "flux", + "response_format": "url" + }' +``` + +2. **b64_json** +```bash +curl -X POST "http://localhost:1337/v1/images/generate" \ + -H "Content-Type: application/json" \ + -d '{ + "prompt": "a white siamese cat", + "model": "flux", + "response_format": "b64_json" + }' +``` + + +### With OpenAI Library + +**You can use the Interference API with the OpenAI Python library by changing the `base_url`:** ```python from openai import OpenAI client = OpenAI( api_key="", - # Change the API base URL to the local interference API - base_url="http://localhost:1337/v1" + base_url="http://localhost:1337/v1" ) response = client.chat.completions.create( model="gpt-3.5-turbo", - messages=[{"role": "user", "content": "write a poem about a tree"}], + messages=[{"role": "user", "content": "Write a poem about a tree"}], stream=True, ) @@ -68,20 +118,20 @@ else: content = token.choices[0].delta.content if content is not None: print(content, end="", flush=True) -``` +``` -## Usage with Requests Library -You can also send requests directly to the Interference API using the requests library. +### With Requests Library -**Send a POST request to `/v1/chat/completions` with the request body containing the model and other parameters:** +**You can also send requests directly to the Interference API using the `requests` library:** ```python import requests url = "http://localhost:1337/v1/chat/completions" + body = { - "model": "gpt-3.5-turbo", + "model": "gpt-3.5-turbo", "stream": False, "messages": [ {"role": "assistant", "content": "What can you do?"} @@ -92,18 +142,20 @@ json_response = requests.post(url, json=body).json().get('choices', []) for choice in json_response: print(choice.get('message', {}).get('content', '')) -``` - +``` ## Key Points -- The Interference API translates OpenAI API requests into G4F provider requests -- You can run it from the PyPI package or the cloned repository -- It supports usage with the OpenAI Python library by changing the `base_url` -- Direct requests can be sent to the API endpoints using libraries like `requests` + - The Interference API translates OpenAI API requests into G4F provider requests. + - It can be run from either the PyPI package or the cloned repository. + - The API supports usage with the OpenAI Python library by changing the `base_url`. + - Direct requests can be sent to the API endpoints using libraries like `requests`. + - Both text and image generation are supported. -**_The Interference API allows easy integration of G4F with existing OpenAI-based applications and tools._** +## Conclusion +The G4F Interference API provides a seamless way to integrate G4F with existing OpenAI-based applications and tools. By following this guide, you should now be able to set up, run, and use the Interference API effectively. Whether you're using it for text generation, image creation, or as a drop-in replacement for OpenAI in your projects, the Interference API offers flexibility and power for your AI-driven applications. + --- |