code: #!/usr/bin/env python
  import os
  
  import lancedb
  from lancedb.embeddings import with_embeddings
  import openai
  import pandas as pd
  from pydantic import BaseModel, Field
  import requests
  
  from aifunctools.openai_funcs import complete_with_functions
  
  openai.api_key = os.getenv("OPENAI_API_KEY")
  
  MODEL = "gpt-3.5-turbo-16k-0613"
  
  db = lancedb.connect(".lancedb")
  
  
  def embed_func(c):
      rs = openai.Embedding.create(input=c, engine="text-embedding-ada-002")
      return [record["embedding"] for record in rs["data"]]
  
  
  def to_lancedb_table(db, memes):
      df = pd.DataFrame([m.model_dump() for m in memes])
      data = with_embeddings(embed_func, df, column="name")
      if "memes" in db.table_names():
          tbl = db.open_table("memes")
          tbl.add(data, mode="overwrite")
      else:
          tbl = db.create_table("memes", data)
      return tbl
  
  
  class Meme(BaseModel):
      id: str = Field(description="The meme id")
      name: str = Field(description="The meme name")
      url: str = Field(description="The meme url")
      width: int = Field(description="The meme image width")
      height: int = Field(description="The meme image height")
      box_count: int = Field(description="The number of text boxes in the meme")
  
  
  def get_memes():
      """
      Get a list of memes from the meme api
      """
      resp = requests.get("https://api.imgflip.com/get_memes")
      return [Meme(**m) for m in resp.json()["data"]["memes"]]
  
  
  def search_memes(query: str):
      """
      Get the most popular memes from imgflip and do a semantic search based on the user query
  
      :param query: str, the search string
      """
      memes = get_memes()
      tbl = to_lancedb_table(db, memes)
      df = tbl.search(embed_func(query)[0]).limit(1).to_df()
      return Meme(**df.to_dict(orient="records")[0]).model_dump()
  
  
  if __name__ == "__main__":
      question = "Please find me the image link for that popular meme with Fry from Futurama"
      print(complete_with_functions(question, search_memes)["choices"][0]["message"]["content"])
  
apis: lancedb.embeddings.with_embeddings lancedb.connect
extract_api: [((263, 290), 'os.getenv', 'os.getenv', (['"""OPENAI_API_KEY"""'], {}), "('OPENAI_API_KEY')\n", (272, 290), False, 'import os\n'), ((331, 358), 'lancedb.connect', 'lancedb.connect', (['""".lancedb"""'], {}), "('.lancedb')\n", (346, 358), False, 'import lancedb\n'), ((389, 454), 'openai.Embedding.create', 'openai.Embedding.create', ([], {'input': 'c', 'engine': '"""text-embedding-ada-002"""'}), "(input=c, engine='text-embedding-ada-002')\n", (412, 454), False, 'import openai\n'), ((614, 660), 'lancedb.embeddings.with_embeddings', 'with_embeddings', (['embed_func', 'df'], {'column': '"""name"""'}), "(embed_func, df, column='name')\n", (629, 660), False, 'from lancedb.embeddings import with_embeddings\n'), ((883, 915), 'pydantic.Field', 'Field', ([], {'description': '"""The meme id"""'}), "(description='The meme id')\n", (888, 915), False, 'from pydantic import BaseModel, Field\n'), ((932, 966), 'pydantic.Field', 'Field', ([], {'description': '"""The meme name"""'}), "(description='The meme name')\n", (937, 966), False, 'from pydantic import BaseModel, Field\n'), ((982, 1015), 'pydantic.Field', 'Field', ([], {'description': '"""The meme url"""'}), "(description='The meme url')\n", (987, 1015), False, 'from pydantic import BaseModel, Field\n'), ((1033, 1074), 'pydantic.Field', 'Field', ([], {'description': '"""The meme image width"""'}), "(description='The meme image width')\n", (1038, 1074), False, 'from pydantic import BaseModel, Field\n'), ((1093, 1135), 'pydantic.Field', 'Field', ([], {'description': '"""The meme image height"""'}), "(description='The meme image height')\n", (1098, 1135), False, 'from pydantic import BaseModel, Field\n'), ((1157, 1214), 'pydantic.Field', 'Field', ([], {'description': '"""The number of text boxes in the meme"""'}), "(description='The number of text boxes in the meme')\n", (1162, 1214), False, 'from pydantic import BaseModel, Field\n'), ((1303, 1352), 'requests.get', 'requests.get', (['"""https://api.imgflip.com/get_memes"""'], {}), "('https://api.imgflip.com/get_memes')\n", (1315, 1352), False, 'import requests\n'), ((1913, 1960), 'aifunctools.openai_funcs.complete_with_functions', 'complete_with_functions', (['question', 'search_memes'], {}), '(question, search_memes)\n', (1936, 1960), False, 'from aifunctools.openai_funcs import complete_with_functions\n')]