lb-reranker-0.5B-v1.0
113
74
license:apache-2.0
by
lightblue
Language Model
OTHER
0.5B params
New
113 downloads
Early-stage
Edge AI:
Mobile
Laptop
Server
2GB+ RAM
Mobile
Laptop
Server
Quick Summary
AI model with specialized capabilities.
Device Compatibility
Mobile
4-6GB RAM
Laptop
16GB RAM
Server
GPU
Minimum Recommended
1GB+ RAM
Code Examples
[6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923][6.66570732 1.86686378 1.01102923]pythonvllm
from vllm import LLM, SamplingParams
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id].logprob) if tok_id in logprob_dict.keys() else 0
llm = LLM("lightblue/lb-reranker-v1.0")
sampling_params = SamplingParams(temperature=0.0, logprobs=14, max_tokens=1)
tok = llm.llm_engine.tokenizer.tokenizer
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = llm.chat(chats, sampling_params)
probs = np.array([[get_prob(r.outputs[0].logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66570732 1.86686378 1.01102923]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205]Un-comment this if running in a Jupyter notebook, Colab etc.python
# Un-comment this if running in a Jupyter notebook, Colab etc.
# import nest_asyncio
# nest_asyncio.apply()
from lmdeploy import GenerationConfig, ChatTemplateConfig, pipeline
import numpy as np
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_prob(logprob_dict, tok_id):
return np.exp(logprob_dict[tok_id]) if tok_id in logprob_dict.keys() else 0
pipe = pipeline(
"lightblue/lb-reranker-v1.0",
chat_template_config=ChatTemplateConfig(
model_name='qwen2d5',
capability='chat'
)
)
tok = pipe.tokenizer.model
idx_tokens = [tok.encode(str(i))[0] for i in range(1, 8)]
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
chats = [make_reranker_inference_conversation(c, q) for q, c in query_texts]
responses = pipe(
chats,
gen_config=GenerationConfig(temperature=1.0, logprobs=14, max_new_tokens=1, do_sample=True)
)
probs = np.array([[get_prob(r.logprobs[0], y) for y in idx_tokens] for r in responses])
N = probs.shape[1]
M = probs.shape[0]
idxs = np.tile(np.arange(1, N + 1), M).reshape(M, N)
expected_vals = (probs * idxs).sum(axis=1)
print(expected_vals)
# [6.66415229 1.84342025 1.01133205][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508][6.64866580, 1.85144404, 1.010719508]pythonopenai
from openai import OpenAI
import numpy as np
from multiprocessing import Pool
from tqdm.auto import tqdm
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Change this to an access token from https://huggingface.co/settings/tokens
)
def make_reranker_input(t, q):
return f"<<<Query>>>\n{q}\n\n<<<Context>>>\n{t}"
def make_reranker_inference_conversation(context, question):
system_message = "Given a query and a piece of text, output a score of 1-7 based on how related the query is to the text. 1 means least related and 7 is most related."
return [
{"role": "system", "content": system_message},
{"role": "user", "content": make_reranker_input(context, question)},
]
def get_reranker_score(context_question_tuple):
question, context = context_question_tuple
messages = make_reranker_inference_conversation(context, question)
completion = client.chat.completions.create(
model="lightblue/lb-reranker-0.5B-v1.0",
messages=messages,
max_tokens=1,
temperature=0.0,
logprobs=True,
top_logprobs=5, # Max allowed by the openai API as top_n_tokens must be >= 0 and <= 5. If this gets changed, fix to > 7.
)
logprobs = completion.choices[0].logprobs.content[0].top_logprobs
calculated_score = sum([int(x.token) * np.exp(x.logprob) for x in logprobs])
return calculated_score
query_texts = [
("What is the scientific name of apples?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the Chinese word for 'apple'?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
("What is the square root of 999?", "An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica)."),
]
with Pool(processes=16) as p: # Allows for parallel processing
expected_vals = list(tqdm(p.imap(get_reranker_score, query_texts), total=len(query_texts)))
print(expected_vals)
# [6.64866580, 1.85144404, 1.010719508]Deploy This Model
Production-ready deployment in minutes
Together.ai
Instant API access to this model
Production-ready inference API. Start free, scale to millions.
Try Free APIReplicate
One-click model deployment
Run models in the cloud with simple API. No DevOps required.
Deploy NowDisclosure: We may earn a commission from these partners. This helps keep LLMYourWay free.