CodeLlama - Code Infilling
Dieses Tutorial zeigt, wie Sie CodeLlama (gehostet auf Huggingface PRO Inference Endpoints) zum Auffüllen von Code aufrufen können.
Dies ist eine spezialisierte Aufgabe, die für Code-Modelle typisch ist. Das Modell ist darauf trainiert, den Code (einschließlich Kommentare) zu generieren, der am besten zu einem vorhandenen Präfix und Suffix passt.
Diese Aufgabe ist in den Basis- und Instruktionsvarianten der CodeLlama-Modelle mit **7B** und **13B** verfügbar. Sie ist nicht für die 34B-Modelle oder die Python-Versionen verfügbar.
Verwendung
import os
from litellm import longer_context_model_fallback_dict, ContextWindowExceededError, completion
os.environ["HUGGINGFACE_API_KEY"] = "your-hf-token" # https://huggingface.co/docs/hub/security-tokens
## CREATE THE PROMPT
prompt_prefix = 'def remove_non_ascii(s: str) -> str:\n """ '
prompt_suffix = "\n return result"
### set <pre> <suf> to indicate the string before and after the part you want codellama to fill
prompt = f"<PRE> {prompt_prefix} <SUF>{prompt_suffix} <MID>"
messages = [{"content": prompt, "role": "user"}]
model = "huggingface/codellama/CodeLlama-34b-Instruct-hf" # specify huggingface as the provider 'huggingface/'
response = completion(model=model, messages=messages, max_tokens=500)
Ausgabe
def remove_non_ascii(s: str) -> str:
""" Remove non-ASCII characters from a string.
Args:
s (str): The string to remove non-ASCII characters from.
Returns:
str: The string with non-ASCII characters removed.
"""
result = ""
for c in s:
if ord(c) < 128:
result += c
return result