# Example usage:
evaluation = hallucination_metric.score(
    input="What is the capital of Mars?",
    output="The capital of Mars is Ares City, a bustling metropolis.",
    context=["Mars is a planet in our solar system. It does not currently have any established cities or a designated capital."]
)
print(f"Hallucination Score: {evaluation.value}") # Expected: 1.0 (hallucination detected)
print(f"Reason: {evaluation.reason}")
```

**Key considerations for the implementation:**

* **ScoreResult Output**: [`Hallucination.score()`](https://www.comet.com/docs/opik/python-sdk-reference/evaluation/metrics/Hallucination.html) returns a ScoreResult object containing the metric name (`name`), score value (`value`), optional explanation (`reason`), metadata (`metadata`), and a failure flag (`scoring_failed`).

## TypeScript: Using Vercel AI SDK Models

The TypeScript SDK integrates seamlessly with the Vercel AI SDK, allowing you to use language models directly with Opik's evaluation metrics. For comprehensive model configuration including supported providers, generation parameters, and advanced settings, see the [Models Reference](/reference/typescript-sdk/evaluation/models).

### Creating Custom Models with OpikBaseModel

For unsupported LLM providers, implement the `OpikBaseModel` interface:

```typescript
import { OpikBaseModel, OpikMessage } from "opik/evaluation/models";

class CustomProviderModel extends OpikBaseModel {
  private apiKey: string;
  private baseUrl: string;

  constructor(modelName: string, apiKey: string, baseUrl: string) {
    super(modelName);
    this.apiKey = apiKey;
    this.baseUrl = baseUrl;
  }

  async generateString(input: string): Promise<string> {
    // Convert string input to message format
    const messages: OpikMessage[] = [
      {
        role: "user",
        content: input,
      },
    ];

    // Call provider API
    const response = await this.generateProviderResponse(messages);

    // Extract text from response
    return response.choices[0].message.content;
  }

  async generateProviderResponse(messages: OpikMessage[]): Promise<unknown> {
    // Make API call to your custom provider
    const response = await fetch(`${this.baseUrl}/chat/completions`, {
      method: "POST",
      headers: {
        Authorization: `Bearer ${this.apiKey}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        model: this.modelName,
        messages: messages,
      }),
    });

    if (!response.ok) {
      throw new Error(`API request failed: ${response.statusText}`);
    }

    return response.json();
  }
}
```

### Using Custom Models

Once implemented, use your custom model like any other:

```typescript
import { Hallucination } from "opik";
import { evaluatePrompt } from "opik";

// Initialize custom model
const customModel = new CustomProviderModel(
  "custom-model-v1",
  process.env.CUSTOM_API_KEY!,
  "https://api.custom-provider.com"
);

// Use with metrics
const metric = new Hallucination({ model: customModel });

const score = await metric.score({
  input: "What is the capital of Mars?",
  output: "The capital of Mars is Ares City, a bustling metropolis.",
  context: [
    "Mars is a planet in our solar system. It does not currently have any established cities or a designated capital.",
  ],
});

console.log(`Hallucination Score: ${score.value}`); // Expected: 1.0 (hallucination detected)
console.log(`Reason: ${score.reason}`);

// Use with evaluatePrompt
await evaluatePrompt({
  dataset,
  messages: [{ role: "user", content: "{{input}}" }],
  model: customModel,
  scoringMetrics: [metric],
});
```

### Best Practices

When implementing custom models:

1. **Implement both required methods**: Ensure your custom model implements both `generateString()` and `generateProviderResponse()` methods
2. **Handle errors gracefully**: Wrap API calls in try-catch blocks and provide meaningful error messages
3. **Configure API keys securely**: Store API keys in environment variables, never hardcode them

For standard model usage and configuration, refer to the [Models Reference](/reference/typescript-sdk/evaluation/models).