0% found this document useful (0 votes)
11 views4 pages

Use Server

Uploaded by

ssstutuorial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Use Server

Uploaded by

ssstutuorial
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

'use server';

/**
* @fileOverview Determines the registration year from a vehicle
registration number using GenAI.
*
* - determineRegistrationYear - A function that determines the
registration year.
* - DetermineRegistrationYearInput - The input type for the
determineRegistrationYear function.
* - DetermineRegistrationYearOutput - The return type for the
determineRegistrationYear function.
*/

import {ai} from '@/ai/genkit';


import {z} from 'genkit';

const DetermineRegistrationYearInputSchema = z.object({


registrationNumber: z
.string()
.describe('The vehicle registration number to determine the year
from.'),
});
export type DetermineRegistrationYearInput = z.infer<typeof
DetermineRegistrationYearInputSchema>;

const DetermineRegistrationYearOutputSchema = z.object({


registrationYear: z
.number()
.describe('The year the vehicle was first registered.'),
});
export type DetermineRegistrationYearOutput = z.infer<typeof
DetermineRegistrationYearOutputSchema>;

export async function determineRegistrationYear(


input: DetermineRegistrationYearInput
): Promise<DetermineRegistrationYearOutput> {
return determineRegistrationYearFlow(input);
}

const prompt = ai.definePrompt({


name: 'determineRegistrationYearPrompt',
input: {schema: DetermineRegistrationYearInputSchema},
output: {schema: DetermineRegistrationYearOutputSchema},
prompt: `Determine the registration year of the vehicle from the provided
registration number.
Registration Number: {{{registrationNumber}}}

Output the registration year as a number.


`,
});

const determineRegistrationYearFlow = ai.defineFlow(


{
name: 'determineRegistrationYearFlow',
inputSchema: DetermineRegistrationYearInputSchema,
outputSchema: DetermineRegistrationYearOutputSchema,
},
async input => {
const {output} = await prompt(input);
return output!;
}
);

from fastapi import FastAPI, HTTPException

from pydantic import BaseModel

from openai import OpenAI

# Initialize OpenAI client

client = OpenAI()

app = FastAPI(title="Vehicle Registration Year API")

# Input schema

class DetermineRegistrationYearInput(BaseModel):

registrationNumber: str

# Output schema
class DetermineRegistrationYearOutput(BaseModel):

registrationYear: int

# Core function

def determine_registration_year(input_data: DetermineRegistrationYearInput) ->


DetermineRegistrationYearOutput:

prompt = f"""

Determine the registration year of the vehicle from the provided registration number.

Registration Number: {input_data.registrationNumber}

Output the registration year as a number only.

"""

response = client.chat.completions.create(

model="gpt-4o-mini", # Fast & cost-effective model

messages=[

{"role": "system", "content": "You are a helpful assistant that extracts registration years from
vehicle numbers."},

{"role": "user", "content": prompt}

],

max_tokens=10

year_text = response.choices[0].message.content.strip()

try:

registration_year = int(year_text)

except ValueError:
raise HTTPException(status_code=400, detail=f"AI returned invalid year: {year_text}")

return DetermineRegistrationYearOutput(registrationYear=registration_year)

# FastAPI endpoint

@app.post("/determine-registration-year", response_model=DetermineRegistrationYearOutput)

async def api_determine_registration_year(input_data: DetermineRegistrationYearInput):

return determine_registration_year(input_data)

You might also like