작업 순차 수행하기

작업 순차 수행하기

이 페이지는 작업을 순차적으로 수행하는 방법을 설명해요. 하나의 작업이 다른 작업의 출력에 의존하는 순차적인 RAG 파이프라인을 살펴볼게요.

출처: 문서

본문

작동 방식 (How it works)

한 작업이 다른 작업의 출력에 의존하도록, RAG 파이프라인이 여러 작업을 순차적으로 수행하게 할 수 있어요.

완전한 코드 예시 (Complete code example)

RAG 작업을 순차적으로 수행하는 예시는 다음과 같아요:

PYTHON

import cohere

co = cohere.ClientV2(api_key="YOUR_COHERE_API_KEY")

# Step 1: Retrieval: embed query, retrieve relevant docs
query = "I need a comprehensive guide on the processing of seafood in the Arctic region"

query_emb = co.embed(
    model="embed-v4.0",
    texts=[query],
    input_type="search_query",
    embedding_types=["float"],
).embeddings.float_[0]

# Step 2: Embedding of documents (assuming docs are already embedded)
doc_emb = [...]

# Step 3: Finding closest documents using dot product of query and document embeddings
scores = [sum(a*b for a,b in zip(query_emb, d)) for d in doc_emb]
top_indices = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)[:3]

# Step 4: Pass the retrieved documents as context to the model
ctx = " ".join([documents[i] for i in top_indices])

response = co.chat(
    model="command-a-03-2025",
    messages=[
        {"role":"system","content":"Answer based on the sources provided."},
        {"role":"user","content": f"Context: {ctx}\n\nQuery: {query}"},
    ],
)

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

다음 단계 (Next Steps)

다음 생성/에이전트 패턴들을 살펴보아요:

더 알아보기 (Learn more)