|
| 1 | +# MistralRephraser |
| 2 | + |
| 3 | +A simple Python class to rephrase sentences using the Mistral AI API. Perfect for generating dynamic feedback |
| 4 | + |
| 5 | +## Quick Setup |
| 6 | + |
| 7 | +1. **Add your API key** in a `.env` file at your project root: |
| 8 | + |
| 9 | +``` |
| 10 | +MISTRAL_API_KEY=your_mistral_api_key |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### 1. Import and create the rephraser |
| 16 | + |
| 17 | +```python |
| 18 | +from mistral import MistralRephraser |
| 19 | + |
| 20 | +rephraser = MistralRephraser() # Uses key from .env |
| 21 | +# or |
| 22 | +# rephraser = MistralRephraser(api_key="your_mistral_api_key") |
| 23 | +``` |
| 24 | + |
| 25 | +### 2. Rephrase a sentence |
| 26 | + |
| 27 | +```python |
| 28 | +sentence = "The cat is on the mat." |
| 29 | +instruction = "Rephrase this sentence in a more formal way." |
| 30 | +result = rephraser.rephrase(sentence, instruction) |
| 31 | +print(result) |
| 32 | +``` |
| 33 | +or with json file |
| 34 | + |
| 35 | +```python |
| 36 | +# Example usage: |
| 37 | +rephraser = MistralRephraser() |
| 38 | +feedback_json = { |
| 39 | + "phase": "shot_release", |
| 40 | + "corrections": [ |
| 41 | + {"joint": "elbow", "correction": 20, "unit": "deg"}, |
| 42 | + {"joint": "knee", "correction": -10, "unit": "deg"} |
| 43 | + ] |
| 44 | +} |
| 45 | +instruction = "Rephrase this feedback in a motivating and clear way for the user." |
| 46 | +print(rephraser.rephrase(feedback_json, instruction)) |
| 47 | +``` |
| 48 | + |
| 49 | +### 3. Change the model (optional) |
| 50 | + |
| 51 | +```python |
| 52 | +rephraser.set_model("mistral-large-latest") |
| 53 | +``` |
| 54 | + |
| 55 | +### 4. Check if your API key is valid |
| 56 | + |
| 57 | +```python |
| 58 | +if not rephraser.is_api_key_valid(): |
| 59 | + print("Invalid API key!") |
| 60 | +``` |
| 61 | + |
| 62 | +## Example: FastAPI Endpoint |
| 63 | + |
| 64 | +```python |
| 65 | +from fastapi import FastAPI, HTTPException |
| 66 | +from mistral import MistralRephraser |
| 67 | + |
| 68 | +app = FastAPI() |
| 69 | +rephraser = MistralRephraser() |
| 70 | + |
| 71 | +@app.post("/rephrase") |
| 72 | +def rephrase_endpoint(data: dict): |
| 73 | + sentence = data["sentence"] |
| 74 | + instruction = data["instruction"] |
| 75 | + try: |
| 76 | + result = rephraser.rephrase(sentence, instruction) |
| 77 | + return {"result": result} |
| 78 | + except Exception as e: |
| 79 | + raise HTTPException(status_code=500, detail=str(e)) |
| 80 | +``` |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +**Ressource:** [https://github.com/mistralai/client-python](https://github.com/mistralai/client-python) |
0 commit comments