Skip to content

Latest commit

 

History

History
131 lines (87 loc) · 2.41 KB

tutorial.md

File metadata and controls

131 lines (87 loc) · 2.41 KB
marp theme _class paginate backgroundColor backgroundImage html
true
gaia
lead
true
true

bg left:40% 80%

MastroGPT Training

Implementing a Chatbot with Nuvolaris

https://www.nuvolaris.io


bg


What is a serverless function?

  • a Python (or other languages) source code

    • Single file or multi file
  • Receives a dict as input and returns a dict as output

    • If it is a web actions, it produces HTML, JSON or others

    • You need to wrap the output in the body field:

{ "body": "<h1>Hello</h1>"}

Implementing a simple chat

A function tht accepts input and return output

  • [1a] Read the input from the args

  • [1b] Return the result as an dictionary: {"output": output}

    • remember to wrap the result in { "body" : ... }
  • [1c] Add the function to the index to use it...

You can chat with the function!


bg


Connecting to OpenAI

from openai import AzureOpenAI
 
ver = "2023-12-01-preview"
# key and host provided by MastroGPT
key = args.get("OPENAI_API_KEY")
host = args.get("OPENAI_API_HOST")
# api call
ai =  AzureOpenAI(
    api_version=ver, 
    api_key=key, 
    azure_endpoint=host)
  • [2a] connect ai with Azure OpenAI and return the api object

Ckecking the connection

# retrieve informations about a model
MODEL = "gpt-35-turbo"
model = ai.models.retrieve(MODEL)
# if ok model.status == 'succeded'
  • [2b] retrieve the model we use, check the status and return 'Welcome.' if is 'succeeded'

  • [2c] add the new chat to the index

Chat with it and verify you get Welcome


bg


Create a request

Structure of a request:

[
   {"role": "system", "content": ROLE},
   {"role": "user", "content": input}
] 
  • [3a] a function to return a request

Invoke a request and return the result

comp = ai.chat.completions.create(
    model=MODEL, 
    messages=request(input, role)
)
  • [3b] invoke the chat completion API
res = comp.choices[0].message.content
  • [3c] read the first message content if any

Now you can chat with the AI