Home Knowledge Base Code Examples and Templates

Code Examples and Templates

LLM API Quick Start Templates

OpenAI Chat Completion

from openai import OpenAI

client = OpenAI()  # Uses OPENAI_API_KEY env var

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
    ],
    max_tokens=500,
    temperature=0.7,
)

print(response.choices[0].message.content)

Anthropic Claude

from anthropic import Anthropic

client = Anthropic()  # Uses ANTHROPIC_API_KEY env var

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, Claude!"}
    ]
)

print(response.content[0].text)

Streaming Response

**OpenAI**
stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a haiku."}],
    stream=True,
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Hugging Face Transformers (Local)

from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "meta-llama/Meta-Llama-3-8B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    device_map="auto",
    torch_dtype="auto"
)

messages = [{"role": "user", "content": "What is the capital of France?"}]
input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
outputs = model.generate(input_ids, max_new_tokens=100)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

RAG Template

from openai import OpenAI
import chromadb

**Setup**
client = OpenAI()
chroma = chromadb.Client()
collection = chroma.create_collection("docs")

**Add documents**
docs = ["Document 1 content...", "Document 2 content..."]
collection.add(
    documents=docs,
    ids=[f"doc_{i}" for i in range(len(docs))]
)

**Query**
def rag_query(question: str, n_results: int = 3):
    results = collection.query(query_texts=[question], n_results=n_results)
    context = "

".join(results["documents"][0])

    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": f"Answer based on context:
{context}"},
            {"role": "user", "content": question}
        ]
    )
    return response.choices[0].message.content

print(rag_query("What does document 1 say?"))

Project Structure Template

<svg viewBox="0 0 410 302" xmlns="http://www.w3.org/2000/svg" style="max-width:100%;height:auto" role="img"><rect x="0" y="0" width="410" height="302" rx="12" fill="#0d1117"/><g font-family="ui-monospace,SFMono-Regular,Menlo,Consolas,&quot;Liberation Mono&quot;,monospace" font-size="14"><text xml:space="preserve" x="20" y="31.7"><tspan fill="#c9d1d9">my_llm_app/</tspan></text><text xml:space="preserve" x="20" y="50.7"><tspan fill="#6e7681">├──</tspan><tspan fill="#c9d1d9"> src/</tspan></text><text xml:space="preserve" x="20" y="69.7"><tspan fill="#6e7681">│</tspan><tspan fill="#c9d1d9">   </tspan><tspan fill="#6e7681">├──</tspan><tspan fill="#c9d1d9"> __init__.py</tspan></text><text xml:space="preserve" x="20" y="88.7"><tspan fill="#6e7681">│</tspan><tspan fill="#c9d1d9">   </tspan><tspan fill="#6e7681">├──</tspan><tspan fill="#c9d1d9"> llm.py          # LLM client wrapper</tspan></text><text xml:space="preserve" x="20" y="107.7"><tspan fill="#6e7681">│</tspan><tspan fill="#c9d1d9">   </tspan><tspan fill="#6e7681">├──</tspan><tspan fill="#c9d1d9"> prompts.py      # Prompt templates</tspan></text><text xml:space="preserve" x="20" y="126.7"><tspan fill="#6e7681">│</tspan><tspan fill="#c9d1d9">   </tspan><tspan fill="#6e7681">├──</tspan><tspan fill="#c9d1d9"> rag.py          # Retrieval logic</tspan></text><text xml:space="preserve" x="20" y="145.7"><tspan fill="#6e7681">│</tspan><tspan fill="#c9d1d9">   </tspan><tspan fill="#6e7681">└──</tspan><tspan fill="#c9d1d9"> api.py          # FastAPI endpoints</tspan></text><text xml:space="preserve" x="20" y="164.7"><tspan fill="#6e7681">├──</tspan><tspan fill="#c9d1d9"> tests/</tspan></text><text xml:space="preserve" x="20" y="183.7"><tspan fill="#6e7681">│</tspan><tspan fill="#c9d1d9">   </tspan><tspan fill="#6e7681">└──</tspan><tspan fill="#c9d1d9"> test_llm.py</tspan></text><text xml:space="preserve" x="20" y="202.7"><tspan fill="#6e7681">├──</tspan><tspan fill="#c9d1d9"> config/</tspan></text><text xml:space="preserve" x="20" y="221.7"><tspan fill="#6e7681">│</tspan><tspan fill="#c9d1d9">   </tspan><tspan fill="#6e7681">└──</tspan><tspan fill="#c9d1d9"> settings.py</tspan></text><text xml:space="preserve" x="20" y="240.7"><tspan fill="#6e7681">├──</tspan><tspan fill="#c9d1d9"> requirements.txt</tspan></text><text xml:space="preserve" x="20" y="259.7"><tspan fill="#6e7681">├──</tspan><tspan fill="#c9d1d9"> .env.example</tspan></text><text xml:space="preserve" x="20" y="278.7"><tspan fill="#6e7681">└──</tspan><tspan fill="#c9d1d9"> README.md</tspan></text></g></svg>
examplessample codetemplateboilerplate

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.