Files
mem0/notebooks/openai.ipynb
T

4.2 KiB

Cookbook for using OpenAI with Embedchain

Step-1: Install embedchain package

In [ ]:
!pip install embedchain
In [ ]:
!pip install embedchain[dataloaders]

Step-2: Set OpenAI environment variables

You can find this env variable on your OpenAI dashboard.

In [ ]:
import os
from embedchain import Pipeline as App

os.environ["OPENAI_API_KEY"] = "sk-xxx"

Step-3: Define your llm and embedding model config

In [ ]:
config = """
llm:
  provider: openai
  config:
    model: gpt-35-turbo
    temperature: 0.5
    max_tokens: 1000
    top_p: 1
    stream: false

embedder:
  provider: openai
  config:
    model: text-embedding-ada-002
    deployment_name: ec_embeddings_ada_002
"""

# Write the multi-line string to a YAML file
with open('openai.yaml', 'w') as file:
    file.write(config)

Step-4 Create embedchain app based on the config

In [ ]:
app = App.from_config(yaml_path="openai.yaml")

Step-5: Add data sources to your app

In [ ]:
app.add("https://www.forbes.com/profile/elon-musk")
In [ ]:
while(True):
    question = input("Enter question: ")
    if question in ['q', 'exit', 'quit']:
        break
    answer = app.query(question)
    print(answer)