Media playback is unsupported on your device Media caption Mr Obama hailed the approaching end of the war and thanked US troops

US President Barack Obama has pledged to "finish the job" and end the Afghan war, addressing the US public live from a military base in Afghanistan.

Speaking a year after Osama Bin Laden's killing, Mr Obama thanked US troops and hailed plans to end combat operations.

He arrived in Kabul on a surprise visit lasting a few hours to sign an accord on future Afghan-US ties with President Hamid Karzai, ahead of a Nato summit.

Hours after his speech, at least seven people died in an attack in Kabul.

Afghan officials said at least two suicide bombers targeted a residential compound popular with foreigners in the eastern part of the capital.

They said at least four of those killed were from a nearby school. Seventeen people were wounded. The Taliban later claimed responsibility for the attacks.

A spokesman for the Nato-led force praised the Afghan security forces for "taking the lead in putting down another desperate attack by insurgents".

Analysis The partnership addresses long-term Afghan financial and security needs and aims to make clear the West is not cutting and running. That's an important message for audiences in Nato countries, as well as in Afghanistan. But it also sends a signal to the militants and their supporters eyeing up the "end game". More than 20 drafts were exchanged before the final text could be agreed. Clauses were also inserted late on to try to reassure neighbours like Iran and Pakistan who are nervous about a continued US presence. Change will be gradual. The US will retain a large degree of influence - but less and less control - over key areas like prisons and night raids. Crucially for Afghans, they now have a framework for gaining control of these issues. The deal will not itself provide a durable peace - that will need many other steps, including a settlement with the insurgent groups and the co-operation of Afghanistan's neighbours.

'National security'

Earlier, Mr Obama said signing the pact with President Karzai was "a historic moment" for both nations.

His visit and TV address came as the patience of the US public with the war in Afghanistan is wearing thin.

In the speech, beamed back to prime-time evening audiences in the US, the president said that at the forthcoming Nato summit, to be held in Chicago later this month, the alliance would "set a goal for Afghan forces to be in the lead for combat operations across the country next year".

Nato has already committed to withdrawing from combat operations in Afghanistan by the end of 2014.

"I will not keep Americans in harm's way a single day longer than is absolutely required for our national security," Mr Obama said. "But we must finish the job we started in Afghanistan, and end this war responsibly."

Correspondents say Mr Obama's words appear to be aimed at showing American voters he is pursuing a strategy to wind down the war, while reassuring Afghans in the face of a continuing Taliban insurgency.

'Path to peace'

About 23,000 of the 88,000 US troops currently in the country are expected to leave Afghanistan by the summer, with all US and Nato combat troops out by the end of 2014.

"It is time to renew America," Mr Obama said towards the end of his remarks.

"My fellow Americans, we have travelled through more than a decade under the dark cloud of war. Yet here, in the pre-dawn darkness of Afghanistan, we can see the light of a new day on the horizon.

"The Iraq war is over. The number of our troops in harm's way has been cut in half, and more will be coming home soon. We have a clear path to fulfil our mission in Afghanistan, while delivering justice to al-Qaeda."

America has no designs beyond an end to al-Qaeda safe-havens, and respect for Afghan sovereignty Barack Obama, US President In pictures: Kabul suicide attack

During the speech, Mr Obama outlined the agreement he had just signed with Mr Karzai.

The BBC's Paul Adams says 20 months of negotiation finally produced an agreement after differences over night raids by Nato special forces and the handling of prisoners were ironed out.

According to the US president, the document outlines plans for training Afghan forces and supporting counter-terrorism efforts, as well as "Afghan commitments to transparency and accountability".

Mr Obama also spoke of a "negotiated peace" with the Taliban, saying that if insurgents broke with al-Qaeda, and followed the "path to peace", there could be reconciliation.

He said that ahead of the Chicago meeting of Nato, he had made it clear to Pakistan that it could be an "equal partner in the process".

Pakistan and US relations soured after Mr Obama launched the raid that killed Bin Laden inside the country's border.

"In pursuit of a durable peace, America has no designs beyond an end to al-Qaeda safe-havens, and respect for Afghan sovereignty."

Mr Obama also rejected calls to leave Afghanistan in advance of the 2014 Nato timeline, saying "we must finish the job we started in Afghanistan, and end this war responsibly".

President Karzai said after the meeting: "By signing this document, we close the last 10 years and open a new season of equal relations."

The Taliban accused him of selling Afghan sovereignty to the Americans.

In the wake of the agreement, the US is to designate Afghanistan as a major non-Nato ally, US officials are quoted as saying by Reuters news agency.

The deal does not commit the US to any specific spending or troop presence, but does leave the way open for Washington to keep forces in Afghanistan until 2024, the Associated Press reports.