# 💻 Examples

#### Chat Completion Example

```python
from openai import AsyncOpenAI

client = AsyncOpenAI(
    api_key='fresed-...',
    base_url='https://fresedgpt.space/v1'
)

async def test_create_chat_completion():
    stream = await client.chat.completions.create(
        messages=[{'role': 'user', 'content': 'hi'}],
        model='gpt-4',
        stream=True
    )

    async for chunk in stream:
        print(chunk.choices[0].delta.content or '', end='')

if __name__ == '__main__':
    import asyncio
    asyncio.run(test_create_chat_completion())
```

#### Image Generation Example

```python
from openai import OpenAI

client = OpenAI(base_url='https://fresedgpt.space/v1', api_key='fresed-...')

response = client.images.generate(
    model="midjourney",
    prompt="a white siamese cat",
    size="1024x1024"
)

print(response)
```

#### Speech Generation Example

```python
from openai import OpenAI

client = OpenAI(
    api_key='fresed-...',
    base_url='https://fresedgpt.space/v1'
)

response = client.audio.speech.create(
    model="tts-1",
    voice="alloy",
    input="hi"
)

print(response)
response.stream_to_file("output.mp3")
```

**Temporary Email Example**

```python
import httpx
import asyncio

BASE_URL = "https://fresedgpt.space/v1"
API_KEY = "fresed-..."

async def generate_temp_mail():
    async with httpx.AsyncClient() as client:
        headers = {
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json"
        }
        response = await client.post(f"{BASE_URL}/temp_mail/generate", headers=headers)
        if response.status_code == 200:
            data = response.json()
            print("Generated Email:", data["email"])
            print("Identifier:", data["identifier"])
            return data["identifier"]
        else:
            print("Failed to generate email:", response.text)
            return None

async def get_temp_mail_messages(identifier):
    async with httpx.AsyncClient() as client:
        headers = {
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json"
        }
        response = await client.get(f"{BASE_URL}/temp_mail/messages/{identifier}", headers=headers)
        if response.status_code == 200:
            messages = response.json()["messages"]
            print("Messages:", messages)
        else:
            print("Failed to get messages:", response.text)

async def main():
    identifier = await generate_temp_mail()
    if identifier:
        input("Press Enter to check for messages...")
        await get_temp_mail_messages(identifier)

if __name__ == "__main__":
    asyncio.run(main())
```

Suno

```python
import requests
import json
import time

# URL for creating a request to the API
create_url = "https://fresedgpt.space/v1/music/generation"

# URL for checking the status of the request to the API
status_url = "https://fresedgpt.space/v1/music/status/"

# Headers for authorization
headers = {
    'Authorization': 'Bearer fresed-...',
    'Content-Type': 'application/json'
}

# Request body with parameters
payload = {
    "prompt": "Create a beautiful song",
    "make_instrumental": False  # or False if instrumental is not required
}

# Convert the request body to a JSON string
data = json.dumps(payload)

# Send a POST request to the API to create a task
response = requests.post(create_url, headers=headers, data=data)

if response.status_code == 200:
    request_id = response.json().get("request_id")
    print(f"Task created successfully. Task ID: {request_id}")

    # Check the status of the task every 10 seconds
    while True:
        status_response = requests.get(f"{status_url}{request_id}", headers=headers)
        if status_response.status_code == 200:
            status_data = status_response.json()
            if status_data.get("status") == "complete":
                print("Music generated successfully:", status_data.get("result"))
                break
            else:
                print("Task is in progress...")
        else:
            print("Error getting task status:", status_response.status_code, status_response.text)
        
        time.sleep(10)
else:
  print("Error:", response.status_code, response.text)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fresed-api.gitbook.io/fresed-api/examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
