> 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

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)
   * यदि आवश्यक हो तो एनवायरनमेंट वेरिएबल जोड़ें
   * स्टार्टअप कमांड दर्ज करें
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.5.1-cuda12.4-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": "You are a helpful AI assistant."},
    {"role": "user", "content": "Explain the difference between TCP and 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|>\nDescribe this diagram in detail."}
]

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 कार्यान्वयन लिखें:
- सम्मिलित करें
- खोज
- हटाएँ
- इन-ऑर्डर ट्रैवर्सल
टाइप हिंट्स और डॉकस्टリング्स शामिल करें।
"""}
]
```

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

```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="You are a helpful assistant.", label="System"),
        gr.Slider(0.1, 1.5, value=0.7, label="Temperature")
    ],
    title="Phi-4 Chat"
)

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-bit) | 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()` सही फ़ॉर्मेटिंग के लिए
* जांचें कि आप instruct संस्करण का उपयोग कर रहे हैं, base का नहीं

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

सामान्य 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) *वर्तमान दरों के लिए।*

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

* उपयोग करें **स्पॉट** लचीले वर्कलोड के लिए मार्केट (अक्सर 30-50% सस्ता)
* भुगतान करें **CLORE** टोकन के साथ
* विभिन्न प्रदाताओं के बीच कीमतों की तुलना करें

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

* गणित ट्यूटरिंग
* कोड सहायता
* दस्तावेज़ विश्लेषण (दृष्टि)
* कुशल एज पर तैनाती
* लागत-कुशल इनफेरेंस

## अगले कदम

* 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.
