Skip to content

Instantly share code, notes, and snippets.

@codebanesr
Last active December 18, 2024 17:17
Show Gist options
  • Save codebanesr/cc3f88fa1570272f69bb2a0f37668df8 to your computer and use it in GitHub Desktop.
Save codebanesr/cc3f88fa1570272f69bb2a0f37668df8 to your computer and use it in GitHub Desktop.
Ai service to extract information from a resume
import axios from 'axios'
import { ReadStream } from 'fs'
import OpenAI from 'openai'
import { ZodType } from 'zod'
import {
  OpenaiGenerateTextOptions,
  OpenaiProvider,
} from './providers/openai/openai.provider'

class Service {
  private openai = new OpenaiProvider()
  baseUrl = 'https://api.deepgram.com/v1/listen';


  async generateText(options: OpenaiGenerateTextOptions): Promise<string> {
    return this.openai.generateText(options)
  }

  async generateJson<SchemaType extends ZodType>(
    instruction: string,
    content: string,
    schema: SchemaType,
    attachmentUrls?: string[],
  ) {
    return this.openai.generateJson<SchemaType>(
      instruction,
      content,
      schema,
      attachmentUrls,
    )
  }

  async generateImage(prompt: string): Promise<string> {
    return this.openai.generateImage(prompt)
  }

  async fromAudioToText(readStream: ReadStream): Promise<string> {
    return this.openai.fromAudioToText(readStream)
  }

  async fromTextToAudio(text: string): Promise<Buffer> {
    return this.openai.fromTextToAudio(text)
  }


  async extractInformation(args: { location: string }) {
    // send this to excel sheet here
    console.log(args)
    return args;
  }


  async extractResumeInformation(resumeText: string) {
    const client = new OpenAI({
      "apiKey": process.env.SERVER_OPENAI_API_KEY
    });
    const runner = client.beta.chat.completions.runTools({
      response_format: {
        "type": "json_object"
      },
      messages: [
        { role: 'system', content: "You are an AI assistant tasked with extracting specific information from a resume using the provided tools. Your goal is to accurately extract and organize relevant details from the resume text. Extract the json" },
        { role: "user", content: resumeText }
      ],
      stream: false,
      model: "gpt-4o",
      "tools": [
        {
          type: 'function',
          function: {
            function: this.extractInformation,
            description: 'Extracts information from a given resume',
            parse: JSON.parse, // or use a validation library like zod for typesafe parsing.
            parameters: {
              "type": "object",
              "properties": {
                "No": {
                  "type": "integer",
                  "minimum": 1
                },
                "Name": {
                  "type": "string"
                },
                "Mobile Number": {
                  "type": "string",
                  "pattern": "^60\\d+$"
                },
                "Outreach Status": {
                  "type": "string",
                  "enum": ["", "Contacted", "Not Contacted", "Pending"]
                },
                "Interview Date": {
                  "type": "string",
                  "format": "date",
                  "enum": [""]
                },
                "Client": {
                  "type": "string",
                  "enum": [""]
                },
                "Nationality": {
                  "type": "string"
                },
                "Highest Education": {
                  "type": "string",
                  "enum": [
                    "SPM",
                    "Diploma in Nursing",
                    "Degree in Nursing",
                    "Master in Nursing",
                    "Non-Nursing Related",
                    "Diploma in Medical Assistant"
                  ]
                },
                "Work Specialization": {
                  "type": "string",
                  "default": "Undefined",
                  "enum": [
                    "Ward - Surgical",
                    "Ward - Medical",
                    "Ward - Maternity",
                    "Ward - Pediatric",
                    "Ward - Oncology",
                    "Ward - Neuro/Stroke",
                    "ICU/HDU/CCU",
                    "OT",
                    "CSSD",
                    "Emergency",
                    "Dialysis",
                    "Outpatient Clinic",
                    "Health Screening Center",
                    "Labor Room",
                    "Nursery",
                    "Cardiac Cath Lab",
                    "Daycare & Endoscopy",
                    "Imaging",
                    "M - Nurse manager",
                    "M - Educator",
                    "M - ADON/DDON/DON",
                    "Geriatric",
                    "Multidisciplinary",
                    "Haematology-oncology",
                    "Stem Cell Transplant",
                    "Infection Control",
                    "Daycare Surgery"
                  ]
                },
                "Method of Communication": {
                  "description": "Method of communication",
                  "type": "string",
                  "enum": [""]
                },
                "Post-basic or Additional Qualification": {
                  "type": "string"
                },
                "Current Company Name": {
                  "type": "string",
                  "description": "The latest company name"
                },
                "Current Department": {
                  "type": "string",
                  "default": "Undefined",
                  "enum": [
                    "Ward - Surgical",
                    "Ward - Medical",
                    "Ward - Maternity",
                    "Ward - Pediatric",
                    "Ward - Oncology",
                    "Ward - Neuro/Stroke",
                    "ICU/HDU/CCU",
                    "OT",
                    "CSSD",
                    "Emergency",
                    "Dialysis",
                    "Outpatient Clinic",
                    "Health Screening Center",
                    "Labor Room",
                    "Nursery",
                    "Cardiac Cath Lab",
                    "Daycare & Endoscopy",
                    "Imaging",
                    "M - Nurse manager",
                    "M - Educator",
                    "M - ADON/DDON/DON",
                    "Geriatric",
                    "Multidisciplinary",
                    "Haematology-oncology",
                    "Stem Cell Transplant",
                    "Infection Control",
                    "Daycare Surgery"
                  ]
                },
                "Previous Department 1": {
                  "type": "string",
                  "enum": [
                    "Ward - Surgical",
                    "Ward - Medical",
                    "Ward - Maternity",
                    "Ward - Pediatric",
                    "Ward - Oncology",
                    "Ward - Neuro/Stroke",
                    "ICU/HDU/CCU",
                    "OT",
                    "CSSD",
                    "Emergency",
                    "Dialysis",
                    "Outpatient Clinic",
                    "Health Screening Center",
                    "Labor Room",
                    "Nursery",
                    "Cardiac Cath Lab",
                    "Daycare & Endoscopy",
                    "Imaging",
                    "M - Nurse manager",
                    "M - Educator",
                    "M - ADON/DDON/DON",
                    "Geriatric",
                    "Multidisciplinary",
                    "Haematology-oncology",
                    "Stem Cell Transplant",
                    "Infection Control",
                    "Daycare Surgery"
                  ]
                },
                "Previous Department 2": {
                  "type": "string",
                  "enum": [
                    "Ward - Surgical",
                    "Ward - Medical",
                    "Ward - Maternity",
                    "Ward - Pediatric",
                    "Ward - Oncology",
                    "Ward - Neuro/Stroke",
                    "ICU/HDU/CCU",
                    "OT",
                    "CSSD",
                    "Emergency",
                    "Dialysis",
                    "Outpatient Clinic",
                    "Health Screening Center",
                    "Labor Room",
                    "Nursery",
                    "Cardiac Cath Lab",
                    "Daycare & Endoscopy",
                    "Imaging",
                    "M - Nurse manager",
                    "M - Educator",
                    "M - ADON/DDON/DON",
                    "Geriatric",
                    "Multidisciplinary",
                    "Haematology-oncology",
                    "Stem Cell Transplant",
                    "Infection Control",
                    "Daycare Surgery"
                  ]
                },
                "Current Work Location": {
                  "type": "string",
                  "enum": [
                    "M - Kuala Lumpur",
                    "M - Perak",
                    "M - Pahang",
                    "M - Selangor",
                    "M - Sarawak",
                    "M - Johor",
                    "M - Melaka",
                    "M - Kelantan",
                    "M - Sabah",
                    "M - Penang",
                    "M - Kedah",
                    "M - Negeri Sembilan",
                    "M - Terengganu",
                    "M - Perlis",
                    "Oversea - Singapore",
                    "Oversea - Saudi",
                    "Oversea - US",
                    "Oversea - UK",
                    "Oversea - Australia",
                    "Oversea - India"
                  ]
                },
                "Preferred Work Location 1": {
                  "type": "string",
                  "enum": [""]
                },
                "Preferred Work Location 2": {
                  "type": "string",
                  "enum": [""]
                }
              },
              "required": [
                "Name",
                "Mobile Number",
                "Nationality",
                "Highest Education",
                "Post-basic or Additional Qualification",
                "Current Company Name",
                "Current Department",
                "Previous Department 1",
                "Previous Department 2",
                "Current Work Location",
                "Work Specialization"
              ]
            },
          },
        },
      ]
    })


    const finalContent = await runner.finalContent();
    return finalContent;
  }


  // will detect all languages except arabic. we will use whisper for it.
  async transcribeAudio(audioUrl: string, language: string): Promise<string> {
    try {
      const isArabic = [
        'ar_AE', 'ar_SA', 'ar_EG', 'ar_LB', 'ar_DZ', 'ar_MA', 'ar_IQ', 'ar_KW', 'ar_QA', 'ar_JO'
      ].includes(language);

      const url = `${this.baseUrl}?smart_format=true${isArabic ? `&language=${language.replace('_', '-')}` : '&detect_language=true'}&model=nova-2`;

      const response = await axios.post(url, { url: audioUrl }, {
        headers: {
          'Authorization': `Token ${process.env.DEEPGRAM_API_KEY}`,
          'Content-Type': 'application/json',
        },
      });

      const transcript = response.data?.results?.channels?.[0]?.alternatives?.[0]?.transcript;
      if (transcript) {
        return transcript;
      } else {
        throw new Error('Unexpected response format');
      }
    } catch (error) {
      console.error('Error transcribing audio:', error);
      throw error;
    }
  }

  isActive(): boolean {
    return this.openai.isActive()
  }
}

export const AiService = new Service()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment