add fix to HF endpoint

This commit is contained in:
Henry
2023-07-08 21:31:43 +01:00
parent 6a7e378baf
commit 3cda348e95
3 changed files with 21 additions and 14 deletions
@@ -78,9 +78,7 @@ export class HuggingFaceInference extends LLM implements HFInput {
async _call(prompt: string, options: this['ParsedCallOptions']): Promise<string> { async _call(prompt: string, options: this['ParsedCallOptions']): Promise<string> {
const { HfInference } = await HuggingFaceInference.imports() const { HfInference } = await HuggingFaceInference.imports()
const hf = new HfInference(this.apiKey) const hf = new HfInference(this.apiKey)
if (this.endpoint) hf.endpoint(this.endpoint) const obj: any = {
const res = await this.caller.callWithOptions({ signal: options.signal }, hf.textGeneration.bind(hf), {
model: this.model,
parameters: { parameters: {
// make it behave similar to openai, returning only the generated text // make it behave similar to openai, returning only the generated text
return_full_text: false, return_full_text: false,
@@ -91,7 +89,13 @@ export class HuggingFaceInference extends LLM implements HFInput {
repetition_penalty: this.frequencyPenalty repetition_penalty: this.frequencyPenalty
}, },
inputs: prompt inputs: prompt
}) }
if (this.endpoint) {
hf.endpoint(this.endpoint)
} else {
obj.model = this.model
}
const res = await this.caller.callWithOptions({ signal: options.signal }, hf.textGeneration.bind(hf), obj)
return res.generated_text return res.generated_text
} }
@@ -30,12 +30,11 @@ export class HuggingFaceInferenceEmbeddings extends Embeddings implements Huggin
async _embed(texts: string[]): Promise<number[][]> { async _embed(texts: string[]): Promise<number[][]> {
// replace newlines, which can negatively affect performance. // replace newlines, which can negatively affect performance.
const clean = texts.map((text) => text.replace(/\n/g, ' ')) const clean = texts.map((text) => text.replace(/\n/g, ' '))
return this.caller.call(() => const obj: any = {
this.client.featureExtraction({ inputs: clean
model: this.model, }
inputs: clean if (!this.endpoint) obj.model = this.model
}) return this.caller.call(() => this.client.featureExtraction(obj)) as Promise<number[][]>
) as Promise<number[][]>
} }
embedQuery(document: string): Promise<number[]> { embedQuery(document: string): Promise<number[]> {
@@ -78,9 +78,7 @@ export class HuggingFaceInference extends LLM implements HFInput {
async _call(prompt: string, options: this['ParsedCallOptions']): Promise<string> { async _call(prompt: string, options: this['ParsedCallOptions']): Promise<string> {
const { HfInference } = await HuggingFaceInference.imports() const { HfInference } = await HuggingFaceInference.imports()
const hf = new HfInference(this.apiKey) const hf = new HfInference(this.apiKey)
if (this.endpoint) hf.endpoint(this.endpoint) const obj: any = {
const res = await this.caller.callWithOptions({ signal: options.signal }, hf.textGeneration.bind(hf), {
model: this.model,
parameters: { parameters: {
// make it behave similar to openai, returning only the generated text // make it behave similar to openai, returning only the generated text
return_full_text: false, return_full_text: false,
@@ -91,7 +89,13 @@ export class HuggingFaceInference extends LLM implements HFInput {
repetition_penalty: this.frequencyPenalty repetition_penalty: this.frequencyPenalty
}, },
inputs: prompt inputs: prompt
}) }
if (this.endpoint) {
hf.endpoint(this.endpoint)
} else {
obj.model = this.model
}
const res = await this.caller.callWithOptions({ signal: options.signal }, hf.textGeneration.bind(hf), obj)
return res.generated_text return res.generated_text
} }