OpenAI langChain

From bibbleWiki
Jump to navigation Jump to search

Introduction

First dip into OpenAI and pretty easy to use. I went to the [freeCodeCamp]. The source for the demos was at youtube and demonstrated

  • streamlit web based gui tool
  • langChain OpenAI language API

The youtube example provides a page where you enter the URL and can ask a query of the text. There are loads of other loaders aside from youtube but this was impressive. Still hate python but this is the other example asking for pet name and color suggestions

from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

from dotenv import load_dotenv

load_dotenv()

def generate_pet_name(animal_type, animal_color):

    llm = OpenAI(temperature=0.7)

    prompt_template_name = PromptTemplate(
        input_variables=['animal_type','animal_color'],
        template="I have a {animal_type} pet and I want a cool name for it, it is {animal_color} in color . Suggest me five cool names for my pet."
    )

    name_chain = LLMChain(llm=llm, prompt=prompt_template_name, output_key='pet_name')

    response = name_chain({'animal_type':animal_type, 'animal_color':animal_color})

    print(response)
    return response