> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-hi/language-models/phi4.md).

# Phi-4

Clore.ai GPUs पर Microsoft का Phi-4 छोटा भाषा मॉडल चलाएँ

Microsoft के Phi-4 को चलाएँ - एक छोटा लेकिन शक्तिशाली भाषा मॉडल।

{% hint style="success" %}
सभी उदाहरण GPU सर्वरों पर चलाए जा सकते हैं, जिन्हें किराए पर लिया गया है [CLORE.AI मार्केटप्लेस](https://clore.ai/marketplace).
{% endhint %}

## CLORE.AI पर किराए पर लेना

1. विज़िट करें [CLORE.AI मार्केटप्लेस](https://clore.ai/marketplace)
2. GPU प्रकार, VRAM और कीमत के अनुसार फ़िल्टर करें
3. चुनें **ऑन-डिमांड** (स्थिर दर) या **स्पॉट** (बोली मूल्य)
4. अपना ऑर्डर कॉन्फ़िगर करें:
   * Docker इमेज चुनें
   * पोर्ट सेट करें (SSH के लिए TCP, वेब UI के लिए HTTP)
   * यदि आवश्यक हो तो environment variables जोड़ें
   * स्टार्टअप कमांड दर्ज करें
5. भुगतान चुनें: **CLORE**, **BTC**या **USDT/USDC**
6. ऑर्डर बनाएं और डिप्लॉयमेंट की प्रतीक्षा करें

### अपने सर्वर तक पहुँचें

* कनेक्शन विवरण यहाँ खोजें **मेरे ऑर्डर**
* वेब इंटरफ़ेस: HTTP पोर्ट URL का उपयोग करें
* SSH: `ssh -p <port> root@<proxy-address>`

## Phi-4 क्या है?

Microsoft का Phi-4 प्रदान करता है:

* उत्कृष्ट प्रदर्शन के साथ 14B पैरामीटर
* बेंचमार्क्स पर बड़े मॉडलों को पछाड़ता है
* मज़बूत तर्क और गणित
* कुशल अनुमान

## मॉडल वेरिएंट

| मॉडल           | पैरामीटर          | VRAM | विशेषता              |
| -------------- | ----------------- | ---- | -------------------- |
| Phi-4          | 14B               | 16GB | सामान्य              |
| Phi-3.5-mini   | 3.8B              | 4GB  | हल्का                |
| Phi-3.5-MoE    | 42B (6.6B सक्रिय) | 16GB | विशेषज्ञों का मिश्रण |
| Phi-3.5-vision | 4.2B              | 6GB  | दृष्टि               |

## त्वरित डिप्लॉय

**Docker इमेज:**

```
pytorch/pytorch:2.11.0-cuda12.8-cudnn9-runtime
```

**पोर्ट:**

```
22/tcp
8000/http
```

**कमांड:**

```bash
pip install transformers accelerate torch && \\
python phi4_server.py
```

## अपनी सेवा तक पहुँचना

डिप्लॉयमेंट के बाद, अपना `http_pub` URL यहाँ **मेरे ऑर्डर**:

1. पर जाएँ **मेरे ऑर्डर** पेज
2. अपने ऑर्डर पर क्लिक करें
3. खोजें `http_pub` URL (उदा., `abc123.clorecloud.net`)

उपयोग करें `https://YOUR_HTTP_PUB_URL` की बजाय `localhost` नीचे दिए गए उदाहरणों में।

## Ollama का उपयोग

```bash

# Phi-4 चलाएँ
ollama run phi4

# Phi-3.5 mini (तेज़)
ollama run phi3.5

# Phi-3.5 vision
ollama run phi3.5-vision
```

## इंस्टॉलेशन

```bash
pip install transformers accelerate torch
```

## मूल उपयोग

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "microsoft/Phi-4"

tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto",
    trust_remote_code=True
)

messages = [
    {"role": "system", "content": "आप एक सहायक AI सहायक हैं।"},
    {"role": "user", "content": "TCP और UDP के बीच अंतर समझाइए।"}
]

inputs = tokenizer.apply_chat_template(
    messages,
    return_tensors="pt",
    add_generation_prompt=True
).to("cuda")

outputs = model.generate(
    inputs,
    max_new_tokens=512,
    temperature=0.7,
    do_sample=True
)

response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
print(response)
```

## Phi-3.5-Vision

छवि समझने के लिए:

```python
from transformers import AutoModelForCausalLM, AutoProcessor
from PIL import Image
import torch

model_id = "microsoft/Phi-3.5-vision-instruct"

processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto",
    trust_remote_code=True
)

image = Image.open("diagram.png")

messages = [
    {"role": "user", "content": "<|image_1|>\nइस आरेख का विस्तार से वर्णन करें।"}
]

prompt = processor.tokenizer.apply_chat_template(
    messages, tokenize=False, add_generation_prompt=True
)

inputs = processor(prompt, [image], return_tensors="pt").to("cuda")

outputs = model.generate(
    **inputs,
    max_new_tokens=512,
    temperature=0.7
)

response = processor.decode(outputs[0], skip_special_tokens=True)
print(response)
```

## गणित और तर्क

```python
messages = [
    {"role": "user", "content": """
चरण-दर-चरण हल करें:
एक किसान के पास मुर्गियाँ और खरगोश हैं।
कुल सिर: 35
कुल पैर: 94
प्रत्येक जानवर कितने हैं?
"""}
]

# Phi-4 चरण-दर-चरण तर्क में उत्कृष्ट है
```

## कोड जनरेशन

```python
messages = [
    {"role": "user", "content": """
निम्न के साथ बाइनरी सर्च ट्री का एक Python implementation लिखें:
- सम्मिलन
- खोज
- हटाना
- इन-ऑर्डर ट्रैवर्सल
टाइप हिन्ट्स और डॉकस्ट्रिंग्स शामिल करें।
"""}
]
```

## क्वांटाइज़्ड इन्फरेंस

```python
from transformers import AutoModelForCausalLM, BitsAndBytesConfig
import torch

quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.bfloat16
)

model = AutoModelForCausalLM.from_pretrained(
    "microsoft/Phi-4",
    quantization_config=quantization_config,
    device_map="auto",
    trust_remote_code=True
)
```

## Gradio इंटरफ़ेस

```python
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "microsoft/Phi-4"
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_id, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True
)

def chat(message, history, system_prompt, temperature):
    messages = [{"role": "system", "content": system_prompt}]
    for h in history:
        messages.append({"role": "user", "content": h[0]})
        messages.append({"role": "assistant", "content": h[1]})
    messages.append({"role": "user", "content": message})

    inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
    outputs = model.generate(inputs, max_new_tokens=512, temperature=temperature, do_sample=True)

    return tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)

demo = gr.ChatInterface(
    fn=chat,
    additional_inputs=[
        gr.Textbox(value="आप एक सहायक सहायक हैं.", label="सिस्टम"),
        gr.Slider(0.1, 1.5, value=0.7, label="तापमान")
    ],
    title="Phi-4 चैट"
)

demo.launch(server_name="0.0.0.0", server_port=7860)
```

## प्रदर्शन

| मॉडल          | GPU      | टोकन/सेकंड |
| ------------- | -------- | ---------- |
| Phi-3.5-mini  | RTX 3060 | \~100      |
| Phi-3.5-mini  | RTX 4090 | \~150      |
| Phi-4         | RTX 4090 | \~60       |
| Phi-4         | A100     | \~90       |
| Phi-4 (4-बिट) | RTX 3090 | \~40       |

## बेंचमार्क

| मॉडल          | MMLU  | HumanEval | GSM8K |
| ------------- | ----- | --------- | ----- |
| Phi-4         | 84.8% | 82.6%     | 94.6% |
| GPT-4-Turbo   | 86.4% | 85.4%     | 94.2% |
| Llama-3.1-70B | 83.6% | 80.5%     | 92.1% |

*Phi-4 बहुत बड़े मॉडलों की बराबरी करता है या उन्हें पीछे छोड़ता है*

## समस्या निवारण

### "trust\_remote\_code" त्रुटि

* जोड़ें `trust_remote_code=True` को `from_pretrained()`
* यह Phi मॉडलों के लिए आवश्यक है

### दोहराए जाने वाले आउटपुट

* कम तापमान (0.3-0.6)
* repetition\_penalty=1.1 जोड़ें
* उचित चैट टेम्पलेट का उपयोग करें

### मेमोरी संबंधी समस्याएँ

* Phi-4 कुशल है, लेकिन 14B के लिए फिर भी लगभग 8GB चाहिए
* ज़रूरत हो तो 4-बिट क्वांटाइज़ेशन का उपयोग करें
* कॉन्टेक्स्ट लंबाई घटाएँ

### गलत आउटपुट फ़ॉर्मैट

* उपयोग करें `apply_chat_template()` सही फ़ॉर्मैटिंग के लिए
* जाँचें कि आप base के बजाय instruct संस्करण का उपयोग कर रहे हैं

## लागत का अनुमान

CLORE.AI मार्केटप्लेस की सामान्य दरें (2024 तक):

| GPU       | प्रति घंटा दर | प्रति दिन दर | 4-घंटे का सत्र |
| --------- | ------------- | ------------ | -------------- |
| RTX 3060  | \~$0.03       | \~$0.70      | \~$0.12        |
| RTX 3090  | \~$0.06       | \~$1.50      | \~$0.25        |
| RTX 4090  | \~$0.10       | \~$2.30      | \~$0.40        |
| A100 40GB | \~$0.17       | \~$4.00      | \~$0.70        |
| A100 80GB | \~$0.25       | \~$6.00      | \~$1.00        |

*मूल्य प्रदाता और मांग के अनुसार बदलते हैं। देखें* [*CLORE.AI मार्केटप्लेस*](https://clore.ai/marketplace) *वर्तमान दरों के लिए।*

**पैसे बचाएँ:**

* का उपयोग करें **स्पॉट** बाधित किए जा सकने वाले कार्य के लिए मार्केट — लगभग एक-तिहाई सर्वरों की स्पॉट कीमत ऑन-डिमांड से कम होती है (मध्य \~13% छूट), बाकी उससे मेल खाते हैं
* से भुगतान करें **CLORE** टोकन
* विभिन्न प्रदाताओं के बीच कीमतों की तुलना करें

## उपयोग के मामले

* गणित ट्यूटरिंग
* कोड सहायता
* दस्तावेज़ विश्लेषण (vision)
* कुशल edge deployment
* लागत-प्रभावी inference

## अगले चरण

* Qwen2.5 - वैकल्पिक मॉडल
* Gemma 2 - Google का मॉडल
* Llama 3.2 - Meta का मॉडल


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-hi/language-models/phi4.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
