Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

async function call not working (uvicorn, fastapi) #71

Closed
ccomkhj opened this issue Apr 25, 2023 · 10 comments
Closed

async function call not working (uvicorn, fastapi) #71

ccomkhj opened this issue Apr 25, 2023 · 10 comments
Labels
bug Something isn't working

Comments

@ccomkhj
Copy link

ccomkhj commented Apr 25, 2023

馃悰 Bug Report

async function call not working (uvicorn, fastapi)

Code sample

from embedbase_client.client import EmbedbaseClient
import yaml
from fastapi import FastAPI

with open("credential/embedbase.yaml", "r") as f:
    API = yaml.safe_load(f)
embedbase_key = API['EMBEDBASE_KEY']
embedbase_url = API['EMBEDBASE_URL']


app = FastAPI()


@app.get("/")
async def root():
    client = EmbedbaseClient(embedbase_url, embedbase_key)

    client.dataset('basil_greenhouse').search(
        'how to grow basil?', limit=5)
    return {"message": "Hello World"}
uvicorn bug_test:app

Environment

  • OS: Linux
  • Python version, get it with: 3.8.16

Screenshots

RuntimeWarning: Enable tracemalloc to get the object allocation traceback

馃搱 Expected behavior

Return list of datasets

馃搸 Additional context

Using FastAPI

@ccomkhj ccomkhj added the bug Something isn't working label Apr 25, 2023
@ccomkhj
Copy link
Author

ccomkhj commented Apr 25, 2023

it works without async

@github-actions
Copy link

Hello @ccomkhj, thank you for your interest in our work!

If this is a bug report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

@louis030195
Copy link
Contributor

louis030195 commented Apr 25, 2023

@ccomkhj thanks! I had some issues also running sync client in async context, I'll try to investigate this, in the meantime prefer using the async client in async context:

from embedbase_client.client import EmbedbaseAsyncClient
import yaml
from fastapi import FastAPI

with open("credential/embedbase.yaml", "r") as f:
    API = yaml.safe_load(f)
embedbase_key = API['EMBEDBASE_KEY']
embedbase_url = API['EMBEDBASE_URL']


app = FastAPI()


@app.get("/")
async def root():
    client = EmbedbaseAsyncClient(embedbase_url, embedbase_key)

    await client.dataset('basil_greenhouse').search(
        'how to grow basil?', limit=5)
    return {"message": "Hello World"}

If this does not work, feel free to use the REST API directly:

import requests
top_k = limit or 5
search_url = f"https://api.embedbase.xyz/v1/basil_greenhouse/search"
data = requests.post(
    search_url, headers={"Authorization": f"Bearer {embedbase_key}"}, json={"query": "how to grow basil", "top_k": 5}
)
data = res.json()

Please let me know if it solves your issue :)

@ccomkhj
Copy link
Author

ccomkhj commented Apr 25, 2023

thanks for the quick response! it works! 馃憤

@ccomkhj ccomkhj closed this as completed Apr 25, 2023
@ccomkhj
Copy link
Author

ccomkhj commented May 4, 2023

I want to combine multiple results.

This works with sync
client = EmbedbaseClient(embedbase_url, embedbase_key)
but does not work with async because each results is coroutine object
client = EmbedbaseAsyncClient(embedbase_url, embedbase_key)

results_recipe = client.dataset(recipe_id).search(
      question, limit=6
  )  
results_farm = client.dataset(farm_id).search(
      question, limit=1
  )  
results = results_recipe + results_farm

error with async at results = results_recipe + results_farm

TypeError: unsupported operand type(s) for +: 'coroutine' and 'coroutine'

Is there a feature to search from multiple dataset?

@ccomkhj ccomkhj reopened this May 4, 2023
@louis030195
Copy link
Contributor

louis030195 commented May 4, 2023

@ccomkhj

Hey 馃憢

You need to use await otherwise it does not get the value, eg:

async def foo()
  return "bar"

print(foo())
# coroutine

print(await foo())
# bar

So in your context:

results_recipe = await client.dataset(recipe_id).search(
      question, limit=6
  )  
results_farm = await client.dataset(farm_id).search(
      question, limit=1
  )  
results = results_recipe + results_farm

An optimization allowed by async would be to run requests in parallel using asyncio.gather():

[results_recipe, results_farm] = await asyncio.gather(
  *[
    client.dataset(recipe_id).search(
      question, limit=6
    ) ,
    client.dataset(farm_id).search(
      question, limit=1
    )
  ]
)
# ...

This way the code would be twice faster.

Would you use a simpler API, like:

results = await client.dataset(recipe_id, farm_id).search(question, limit=6)

? I guess here it does not make sense because you want a limit of 6, and 1?

@ccomkhj
Copy link
Author

ccomkhj commented May 4, 2023

Thank you for the ready-to run example. @louis030195
It works super fast!

It would be so helpful in my use-case

results = await client.dataset(recipe_id, farm_id, user_id, locaton_id).search(question, max_token=3000, ratio=[.7, .1, .1, .1)

max_token of 2100, 300, 300, 300 are applied to each dataset.

@ccomkhj ccomkhj closed this as completed May 4, 2023
@ashgansh
Copy link
Member

ashgansh commented May 4, 2023

@ccomkhj

results_recipe = client.dataset(recipe_id).search(
      question, limit=6
  )  
results_farm = client.dataset(farm_id).search(
      question, limit=1
  )  

Curious, why you're using multiple datasets? Is there a reason you didn't put everything in a single dataset?

@ccomkhj
Copy link
Author

ccomkhj commented May 4, 2023

Two reasons. @hotkartoffel

  1. Each dataset has a different purpose (I.e. one is about person, one is about company, one is about culture, and so on), so easy to manage diverse datasets.
  2. Guarantee to include something dataset (not ranked by only similarity)

@ashgansh
Copy link
Member

ashgansh commented May 4, 2023

@ccomkhj I see, makes sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants