# Agent Behavior & Custom GPT Instructions

This app can use your **Custom GPT instructions** so the voice agent behaves like your GPT.

---

## 1. Required

- **OPENAI_API_KEY** in `.env`  
  Use the same API key as for your Custom GPT (from [API keys](https://platform.openai.com/api-keys)).

---

## 2. Use Your Custom GPT Behavior

### Option A: Same behavior as Custom GPT (recommended)

1. In [ChatGPT](https://chat.openai.com/) go to your Custom GPT → **Configure** → **Instructions**.
2. Copy the full instructions text.
3. In `laravel/.env` set:
   ```env
   OPENAI_AGENT_INSTRUCTIONS="Paste your full instructions here. Use one line or escape newlines."
   OPENAI_SIMPLE_CHAT=true
   OPENAI_CHAT_MODEL=gpt-4o
   ```
   - Use the **same model** as your Custom GPT (e.g. `gpt-4o`, `gpt-4`, `gpt-3.5-turbo`).
   - For long instructions with newlines, you can use a single line and replace line breaks with `\n` or put the value in a file and load it (see below).

4. Clear config cache: `php artisan config:clear` (if you use caching).

**Result:** The agent uses only your instructions as the system prompt. No state machine, no JSON format — plain chat like your Custom GPT.

### Option B: Custom instructions + built‑in flow

If you want your tone/domain **and** the app’s car-consultant flow (states, progress, JSON):

- Set `OPENAI_AGENT_INSTRUCTIONS` with your text.
- Leave **OPENAI_SIMPLE_CHAT=false** (or omit it).

Your instructions are then **prepended** to the built-in prompt; the model must still return the required JSON (reply, next_state, etc.).

---

## 3. .env reference

| Variable | Required | Description |
|----------|----------|-------------|
| OPENAI_API_KEY | Yes | OpenAI API key (same as Custom GPT). |
| OPENAI_CHAT_MODEL | No | Model name. Default: `gpt-3.5-turbo`. Use e.g. `gpt-4o` to match Custom GPT. |
| OPENAI_AGENT_INSTRUCTIONS | No | System instructions (e.g. copied from Custom GPT). |
| OPENAI_SIMPLE_CHAT | No | Set to `true` to use only your instructions (no state machine). Default: `false`. |
| OPENAI_AGENT_INSTRUCTIONS_FILE | No | Path to a file with instructions (e.g. `storage/app/agent_instructions.txt`). Used if OPENAI_AGENT_INSTRUCTIONS is empty. |
| USE_FREE_TTS | No | Use free Google TTS when `true`. Set `false` to use OpenAI TTS only. |

---

## 4. Multi-line instructions (file)

For long or multi-line instructions, use a file:

1. Create e.g. `laravel/storage/app/agent_instructions.txt`.
2. Paste your full Custom GPT instructions (with line breaks).
3. In `.env` set:
   ```env
   OPENAI_AGENT_INSTRUCTIONS_FILE=storage/app/agent_instructions.txt
   OPENAI_SIMPLE_CHAT=true
   OPENAI_CHAT_MODEL=gpt-4o
   ```
   Leave `OPENAI_AGENT_INSTRUCTIONS` empty or omit it. The file path is relative to the Laravel project root.

---

## 5. Dependencies (already in the project)

- **PHP 8.2+**
- **Laravel 11**
- **openai-php/client** – OpenAI API (Chat, Whisper, TTS)
- **guzzlehttp/guzzle** – HTTP client used by the OpenAI client

No extra packages are needed for Custom GPT–style behavior; only the `.env` settings above.

---

## 6. If the agent still doesn’t match Custom GPT

- Confirm **OPENAI_CHAT_MODEL** matches the model used by your Custom GPT.
- Confirm **OPENAI_AGENT_INSTRUCTIONS** is exactly the same as in the GPT builder (no truncation, same line breaks or `\n`).
- For “exactly like Custom GPT” use **OPENAI_SIMPLE_CHAT=true** so the app doesn’t require the structured JSON flow.
