Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions fastchat/serve/api_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,71 @@ def palm_api_stream_iter(chat, message, temperature, top_p, max_new_tokens):
"error_code": 0,
}
yield data

def ai2_api_stream_iter(model_name,
messages,
temperature,
top_p,
max_new_tokens,
api_key=None,
api_base=None,
):

from requests import post
from json import loads

# get keys and needed values
ai2_key = api_key or os.environ.get('AI2_API_KEY')
api_base = api_base or "https://inferd.allen.ai/api/v1/infer"
model_id = "mov_01hhndc7e8k2s9x379ge8sm8xs"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
model_id = "mov_01hhndc7e8k2s9x379ge8sm8xs"
model_id = "mov_01hj1pj8aqn5s6f0zz1qb1chhn"

FYI @natolambert. This includes the fix for the temperature = 0.0 issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one more update coming. Stay tuned :). We made a bunch of other changes that we're releasing today.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# Make requests
gen_params = {
"model": model_name,
"prompt": messages,
"temperature": temperature,
"top_p": top_p,
"max_new_tokens": max_new_tokens,
}
logger.info(f"==== request ====\n{gen_params}")
res = post(api_base,
stream=True,
headers={
"Authorization": f"Bearer {ai2_key}"
},
json={
"model_version_id": model_id,
# This input format is specific to the Tulu2 model. Other models
# may require different input formats. See the model's schema
# documentation on InferD for more information.
"input": {
"messages": messages,
"opts": {
"max_tokens": max_new_tokens,
"temperature": temperature,
"top_p": top_p,
"logprobs": 1, # increase for more choices
}
}
})

if res.status_code != 200:
logger.error(f"unexpected response ({res.status_code}): {res.text}")
raise ValueError("unexpected response from InferD", res)

text = ""
for line in res.iter_lines():
if line:
part = loads(line)
if "result" in part and "output" in part["result"]:
for t in part["result"]["output"]["text"]:
text += t
else:
logger.error(f"unexpected part: {part}")
raise ValueError("empty result in InferD response")

data = {
"text": text,
"error_code": 0,
}
yield data